Working on CUP. Here are my files: A3.cup:
import java.io.*;
import java_cup.runtime.*;
/* Terminals (tokens returned by the scanner). */
terminal TIMES, DIVIDE, OPAREN, CPAREN, BEGIN, END, RETURN, IF, ELSE, WRITE, READ, MAIN, Id, NEQUALTO, EQUALTO, Num, PLUS, MINUS, INT, REAL, STRING, ASSIGN, SEMI, COM, QString;
/* Non terminals */
non terminal MatchedStmt, UnmatchedStmt, OtherStmt, Stmt, Program, MethodDecl, Block, Type, FormalParams, FormalParam, LocalVarDecl, AssignStmt;
non terminal ReturnStmt, WriteStmt, ReadStmt, Expression, MultiplicativeExpr, PrimaryExpr, BoolExpression, ActualParams;
non terminal NonEmptyActualParams, StatementList, NonEmptyFormalParams;
/* Precedences */
precedence left PLUS, MINUS;
precedence left TIMES, DIVIDE;
/* The grammar */
Program ::= Program MethodDecl
| MethodDecl
;
Type ::= INT | REAL | STRING;
MethodDecl ::= Type MAIN Id OPAREN FormalParams CPAREN Block
| Type Id OPAREN FormalParams CPAREN Block
;
FormalParams ::= /* empty */
| NonEmptyFormalParams
;
NonEmptyFormalParams ::= FormalParam
| NonEmptyFormalParams COM FormalParam
;
FormalParam ::= Type Id;
Block ::= BEGIN StatementList END;
StatementList ::= /* empty */
| StatementList Stmt
;
Stmt ::= MatchedStmt
| UnmatchedStmt
;
MatchedStmt ::= IF OPAREN BoolExpression CPAREN MatchedStmt ELSE MatchedStmt
| OtherStmt
;
UnmatchedStmt ::= IF OPAREN BoolExpression CPAREN Stmt
| IF OPAREN BoolExpression CPAREN MatchedStmt ELSE UnmatchedStmt
;
OtherStmt ::= Block
| LocalVarDecl
| AssignStmt
| ReturnStmt
| WriteStmt
| ReadStmt
;
LocalVarDecl ::= Type Id SEMI | Type AssignStmt;
AssignStmt ::= Id ASSIGN Expression SEMI
| Id ASSIGN QString SEMI
;
ReturnStmt ::= RETURN Expression SEMI;
WriteStmt ::= WRITE OPAREN Expression COM QString CPAREN SEMI;
ReadStmt ::= READ OPAREN Expression COM QString CPAREN SEMI;
Expression ::= MultiplicativeExpr
| Expression PLUS MultiplicativeExpr
| Expression MINUS MultiplicativeExpr
;
MultiplicativeExpr ::= PrimaryExpr
| MultiplicativeExpr TIMES PrimaryExpr
| MultiplicativeExpr DIVIDE PrimaryExpr
;
PrimaryExpr ::= Num
| Id
| OPAREN Expression CPAREN
| Id OPAREN ActualParams CPAREN
;
BoolExpression ::= Expression EQUALTO Expression
| Expression NEQUALTO Expression
;
ActualParams ::= /* empty */
| NonEmptyActualParams
;
NonEmptyActualParams ::= Expression
| NonEmptyActualParams COM Expression
;
A3.lex:
import java_cup.runtime.*;
%%
%implements java_cup.runtime.Scanner
%type Symbol
%function next_token
%class A3Scanner
%eofval{ return null;
%eofval}
%state COMMENT
KEYWORDS = (WRITE|READ|IF|ELSE|RETURN|STRING|BEGIN|END|MAIN|INT|REAL)
Id = [A-Za-z][A-Za-z0-9]*
Num = [0-9]+(\.[0-9]+)?
%%
<YYINITIAL> "/**" { yybegin(COMMENT); }
<YYINITIAL>"+" { return new Symbol(A3Symbol.PLUS); }
<YYINITIAL>"-" { return new Symbol(A3Symbol.MINUS); }
<YYINITIAL>"*" { return new Symbol(A3Symbol.TIMES); }
<YYINITIAL>"/" { return new Symbol(A3Symbol.DIVIDE); }
<YYINITIAL>"MAIN" { return new Symbol(A3Symbol.MAIN); }
<YYINITIAL>"BEGIN" { return new Symbol(A3Symbol.BEGIN); }
<YYINITIAL>"END" { return new Symbol(A3Symbol.END); }
<YYINITIAL>"(" { return new Symbol(A3Symbol.OPAREN); }
<YYINITIAL>")" { return new Symbol(A3Symbol.CPAREN); }
<YYINITIAL>";" { return new Symbol(A3Symbol.SEMI); }
<YYINITIAL>{Id} { return new Symbol(A3Symbol.Id); }
<YYINITIAL>{Num} { return new Symbol(A3Symbol.Num); }
<YYINITIAL> "RETURN" { return new Symbol(A3Symbol.RETURN); }
<YYINITIAL> "IF" { return new Symbol(A3Symbol.IF); }
<YYINITIAL> "ELSE" { return new Symbol(A3Symbol.ELSE); }
<YYINITIAL> "WRITE" { return new Symbol(A3Symbol.WRITE); }
<YYINITIAL> "READ" { return new Symbol(A3Symbol.READ); }
<YYINITIAL> "!=" { return new Symbol(A3Symbol.NEQUALTO); }
<YYINITIAL> "==" { return new Symbol(A3Symbol.EQUALTO); }
<YYINITIAL> "INT" { return new Symbol(A3Symbol.INT); }
<YYINITIAL> "REAL" { return new Symbol(A3Symbol.REAL); }
<YYINITIAL> "STRING" { return new Symbol(A3Symbol.STRING); }
<YYINITIAL> "=" { return new Symbol(A3Symbol.ASSIGN); }
<YYINITIAL> "," { return new Symbol(A3Symbol.COM); }
<YYINITIAL> "\"[^\"]\"" { return new Symbol(A3Symbol.QString); }
<YYINITIAL>.|\n|\r {}
<COMMENT> "**/" { yybegin(YYINITIAL); }
<COMMENT>. {}
<COMMENT>\n|\r {}
\r|\n {}
. {}
A3User.java:
import java.io.*; class A3User {
public static void main(String[] args) throws Exception {
File inputFile = new File ("A3.tiny");
A3Parser parser= new A3Parser(new A3Scanner(new FileInputStream(inputFile)));
Integer result =(Integer)parser.parse().value;
FileWriter fw=new FileWriter(new File("A3.output"));
fw.write("Number of methods: "+ result.intValue());
fw.close();
} }
The Output file should contain:
Number of methods: #
It is not creating the output file and I am not sure why. Using this input: A3.tiny
/** sahftrjhesj ew**
**/
INT f(INT x, INT y )
BEGIN
z:=x;
z := x*x - y*y;
RETURN x+z;
END
INT MAIN f1()
BEGIN
REAL x;
READ(x, "A41.input");
REAL y;
READ(y, "A42.input");
INT z;
z:=0;
IF (x!=y) z := f2(x,y) + f2(y,x);
WRITE (z, "A4.output");
END
It should create: A3.output:
Number of methods: 2
Instead, it gives me the following error even though everything else compiled correctly:
Processing first section -- user code.
Processing second section -- JLex declarations.
Processing third section -- lexical rules.
Creating NFA machine representation.
NFA comprised of 173 states.
Working on character classes.::.::..:::::::....::::::.:.:.::...:.::.:........::................::::.:.:::..........
NFA has 36 distinct character classes.
Creating DFA transition table.
Working on DFA states..............................................................................
Minimizing DFA transition table.
64 states after removal of redundant states.
Outputting lexical analyzer code.
Opening files...
Parsing specification from standard input...
Checking specification...
Building parse tables...
Computing non-terminal nullability...
Computing first sets...
Building state machine...
Filling in tables...
Checking for non-reduced productions...
Writing parser...
Closing files...
------- CUP v0.10k Parser Generation Summary -------
0 errors and 0 warnings
27 terminals, 24 non-terminals, and 51 productions declared,
producing 102 unique parse states.
0 terminals declared but not used.
0 non-terminals declared but not used.
0 productions never reduced.
0 conflicts detected (0 expected).
Code written to "A3Parser.java", and "A3Symbol.java".
---------------------------------------------------- (v0.10k)
Syntax error
Couldn't repair and continue parse
Exception in thread "main" java.lang.Exception: Can't recover from previous error(s)
at java_cup.runtime.lr_parser.report_fatal_error(lr_parser.java:361)
at java_cup.runtime.lr_parser.unrecovered_syntax_error(lr_parser.java:409)
at java_cup.runtime.lr_parser.parse(lr_parser.java:601)
at A3User.main(A3User.java:5)
Let me know if you see anything I need to fix.