100 likes | 256 Views
Assignment 3. Jianguo Lu. /** this is a comment line in the sample program **/ INT f2(INT x, INT y ) BEGIN INT z; z := x*x - y*y; RETURN z; END INT MAIN f1() BEGIN INT x; READ(x, "A41.input"); INT y; READ(y, "A42.input"); INT z;
E N D
Assignment 3 Jianguo Lu
/** this is a comment line in the sample program **/ INT f2(INT x, INT y ) BEGIN INT z; z := x*x - y*y; RETURN z; END INT MAIN f1() BEGIN INT x; READ(x, "A41.input"); INT y; READ(y, "A42.input"); INT z; z := f2(x,y) + f2(y,x); WRITE (z, "A4.output"); END You only need to check the syntax You don’t need to check things such as Whether the types are correct; Whether the program makes sense. How to count methods: program ::= methodDecl {:RESULT= 1;:} | methodDecl program:e {: RESULT=e+1); :} ; Task: check whether the a program is syntactically correct
Modify the EBNF to javaCup Expression -> MultiplicativeExpr (( '+' | '-' ) MultiplicativeExpr)* MultiplicativeExpr -> PrimaryExpr (( '*' | '/' ) PrimaryExpr)* PrimaryExpr -> Num // Integer or Real numbers | Id | '(' Expression ')' | Id '(' ActualParams ')' BoolExpression -> Expression '==' Expression | Expression '!=' Expression ActualParams -> [Expression ( ',' Expression)*] • You need to remove * and [ ]. P::=M | M P ; optionalMain::=MAIN | ; • You don’t need to use the expression definition here • Use precedence rule would be more concise • Try to reduce file sizes.
If you have difficult in locating the errors, send emails to GAs or to me, attaching your lex and cup files and detailed error message. • Test extensively on your program. Those sample inputs are not enough. • Write short programs. Remove anything that you are not sure of. • A3 is based on A2. • Lex file should be enhanced {ID} { return new Symbol(…); } • A4 is based on A3. • Will be tested in exam • important
Common problems: • JavaCup syntax • {: :} for actions • ; after each grammar rule • types for terminals and non terminals program ::= methodDecl {:RESULT= 1;:} | methodDecl program:e {: RESULT=e+1); :} ; • JavaCup semantics • shift/reduce conflicts • empty parameter list, empty statement list • if-then-else • Connection between Jlex and javacup • Terminals not declared in JavaCup
Debugging methods • turn on debugging mode in the parser, by changing parser.parse().value; to parser.debug_parse().value; • Print out symbols in scanner "+" {System.out.println(yytext()); return new Symbol(CalcSymbol.PLUS); } "-" {System.out.println(yytext()); return new Symbol(CalcSymbol.MINUS); } {NUMBER} { System.out.println(yytext()); return new Symbol(CalcSymbol.NUMBER, new Integer(yytext()));} \r|\n|. {System.out.println(yytext()); }
Use debug_parse() luna:~/214>java A3User # Initializing parser # Current Symbol is #10 Syntax error # Attempting error recovery # Finding recovery state on stack # Pop stack by one, state was # 0 # No recovery state found on stack # Error recovery fails 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.debug_parse(lr_parser.java:801) at A3User.main(A3User.java:6)
Check A3Symbol.java //---------------------------------------------------- // The following code was generated by CUP v0.10k // Fri Mar 16 11:28:02 EDT 2007 //---------------------------------------------------- /** CUP generated class containing symbol constants. */ public class A3Symbol { /* terminals */ public static final int TIMES = 21; public static final int READ = 16; public static final int ELSE = 4; public static final int PLUS = 19; public static final int RPAREN = 24; public static final int INT = 11; public static final int EQUAL = 5; public static final int THEN = 3; public static final int SEMI = 15; public static final int NOTEQUAL = 6; public static final int RETURN = 18; public static final int END = 14; public static final int IF = 2; … public static final int ID = 10; …
Error tokens • What if the input is x @+y; • Scanner: <YYINITIAL>. { return new Symbol(ERROR); } • Parser: • Does not need to do anything; • You don’t need to specify rules such as ID ERROR + ID {: some error msg :} • You only need to specify the correct rules, such as ID+ID • When the parser expects to see “+”, but an ERROR token (or any other token) is encountered, the parser knows there is an error
Follow the first example in javacup slides. • the second the and third examples are for assignment 4. • create script in a bat file, say a3.bat in windows: java JLex.Main A3.lex java java_cup.Main -parser A3Parser -symbols A3Symbol < A3Lu.cup javac -classpath ..;. A3Lu.lex.java A3Parser.java A3Symbol.java A3User.java java A3User more A3.output > a3 • in unix, change access mode of the file (say “a3”) to executable % chmod 700 a3 % a3