Presentation is loading. Please wait.

Presentation is loading. Please wait.

CENG444 Recitation Notes Platforms – Linux(recommended) -> runantlr XXX.g -> javac *.java -> java Main That simple! – Windows Prefer Eclipse

Similar presentations


Presentation on theme: "CENG444 Recitation Notes Platforms – Linux(recommended) -> runantlr XXX.g -> javac *.java -> java Main That simple! – Windows Prefer Eclipse"— Presentation transcript:

1 CENG444 Recitation Notes Platforms – Linux(recommended) -> runantlr XXX.g -> javac *.java -> java Main That simple! – Windows Prefer Eclipse http://antlreclipse.sourceforge.net/ How to install antLR plugin for Eclipse is explained

2 Last stable version of antLR is 2.7.7, 2.7.6 is also OK. For classpath in Linux: export CLASSPATH=$CLASSPATH:/usr/share/java/antlr.jar

3 Main Parts Lexer Parser Tree Parser

4 Lexer Characters → Tokens Use regular expressions Conversion to tokens Solve nondeterminism problems newline() protected

5 Lexer class L extends Lexer; options { k=2;//lookahead of two characters charVocabulary='\u0000'..'\u007F'; testLiterals=false;//do not automatically test for literals } tokens {....... } CMNT : "/*“ (options {greedy=false;} :.)* "*/" {$setType(Token.SKIP);};//an example rule: comments in //our project

6 Parser Tokens → Parse Tree Tell the grammar to antLR Eliminate left recursion Choose what will appear in our tree in what position (node or root) A useful link for much better parse error reports: http://www.alittlemadness.com/?p=31

7 Parser class P extends Parser; options { k=2;//lookahead of two characters buildAST =true;//do not forget this for your tree parser!! } program : (d:declaration_list)(f:function_list) {#program = #([PROGRAM,"PROGRAM"],#program);}; //an example rule: program definition in our project.. function:basic_type FUNC^ fid:ID LPAREN! parameter_list RPAREN! function_body ENDFUNC!; //attention to ^ and !

8 Tree Parser Very similar to the parser Walk on the tree – Detect the errors – Evaluate expressions (not until phase 2) For a visual tree view try using ASTFrame class (just for debugging)

9 Tree Parser class TP extends TreeParser; options { buildAST=true; k=1;} {....//any Java code can be written here } program returns [String str=""] //initialization of the return value is a must { //variable declarations are written here } : #(PROGRAM declaration_list function_list) {if(#PROGRAM.getFirstChild().getNextSibling() == null) {err.printError(2,"",scopename);}};


Download ppt "CENG444 Recitation Notes Platforms – Linux(recommended) -> runantlr XXX.g -> javac *.java -> java Main That simple! – Windows Prefer Eclipse"

Similar presentations


Ads by Google