1 / 13

JavaCC

JavaCC. T op-down ( R ecursive descent) . Mest populære mht Java. Regulær grammatik og Context-free grammatik i en fil. Tillader EBNF: (..)*, (..)+ og (..)?. EBNF. P p rogram ->    'program' '(' IdT ')' ( vartypelist )* 'begin' ( statement )* 'end’

olathe
Download Presentation

JavaCC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JavaCC • Top-down (Recursive descent). • Mest populære mht Java. • Regulær grammatik og Context-free grammatik i en fil. • Tillader EBNF: (..)*, (..)+ og (..)?

  2. EBNF Pprogram->    'program' '(' IdT ')' ( vartypelist)* 'begin' (statement)* 'end’ vartypelist-> ('float' | 'int')  IdT varlist ';’ Varlist->(',' IdT)* Statement->IdT '=' expA ';’ Exp->         term (('+' | '-') term)* Term->        sexp (('*' | '/') sexp)* Sexp->        '-' element | element Element->'(' exp ')' | IdT

  3. JavaCC Opbygning af: Fo.jj <javacc_options> PARSER_BEGIN ( <ID> ) <java_compilation_unit> PARSER_END ( <ID> ) ( <production> )*

  4. JavaCC Opbygning af: parser_1.jj options { LOOKAHEAD=1; JAVA_UNICODE_ESCAPE=true; } PARSER_BEGIN(parser_1) public class parser_1 { public static void main(String args[]) throws ParseException { parser_1 parser; parser = new parser_1(System.in); parser.program(); } } PARSER_END(parser_1) ”Grammatik” parser = new parser_1(new java.io.FileInputStream(”filnavn”));

  5. JavaCC compileren. > javacc parser_1.jj > javac parser_1.java > java parser_1 < srcprog.txt parser_1.java parser_1.class javac parser_1.jj javacc parser_1.html jjdoc

  6. Regulær grammatik ["a"-"z"] matcher alle lower case letters ~[] matcher alle character ~["\n","\r"] matcher alle character undtagen new line og return e1 | e2 | e3 Valg mellem e1, e2, e3 (e)+ En eller flera af e (e)* Nul eller flere af e (e)? e er optional Hvor e er elementer fra alfabetet eller et token.

  7. Regulær grammatik SKIP :{” ” |”\r”|”\t”|”\n”} SPECIAL_TOKEN : /* COMMENTS: # En kommentar <crt> */ { <COMMENT: "#" (~["\n","\r"])* ("\n"|"\r"|"\r\n")> } TOKEN : /* PRE DEF */ { < programT: ”program” > | < ifT: ”if" > |< thenT: ”then" > | < elseT: ”else” > | ... } TOKEN : /* SEPARATORS */ { < lparnT: "(" > | < rparnT: ")" > | < semicolonT:";" > | ...

  8. Regulær grammatik TOKEN : {< realT:<intT> ( "." <intT> )? | "." <intT> > | < intT: ( <DIGIT> )+ > | < idT: <LETTER> (<LETTER>|<DIGIT>|"_")* > | < #LETTER:["\u0024","\u0041"-"\u005a", "\u0061"-"\u007a","\u00c0"-"\u00d6", "\u00d8"-"\u00f6","\u00f8"-"\u00ff", "\u0100"-"\u1fff","\u3040"-"\u318f", "\u3300"-"\u337f","\u3400"-"\u3d2d", "\u4e00"-"\u9fff","\uf900"-"\ufaff” ]> | < #DIGIT:["\u0030"-"\u0039","\u0660"-"\u0669", "\u06f0"-"\u06f9","\u0966"-"\u096f", "\u09e6"-"\u09ef","\u0a66"-"\u0a6f", "\u0ae6"-"\u0aef","\u0b66"-"\u0b6f", "\u0be7"-"\u0bef","\u0c66"-"\u0c6f", "\u0ce6"-"\u0cef","\u0d66"-"\u0d6f", "\u0e50"-"\u0e59","\u0ed0"-"\u0ed9", "\u1040"-"\u1049" ] > }

  9. Gramatiske regler void StartSymbol() : {} { <programT> <lparnT> <idT> <rparnT> (statement())* <slutT> {System.out.println("Parsning slut.");} } void statement() : {} { <idT> <equT> exp() <semicolonT> {System.out.println("Statement.");} } void exp() : {Token id;} { term() ( ( id= <plusT> | id=<minusT> ) term() {if (id.kind== plusT) System.out.println("term PLUS term."); if (id.kind== minusT) System.out.println("term MINUS term."); } )* }

  10. Objektet: Token. int kind; int beginLine, beginColumn, endLine, endColumn; String image; Token next; ----------------------------------- Token parser.getNextToken() throws ParseError;

  11. Symboltabel class SymData { public int type; public float fvalue; } static java.util.Hashtable SymTabel= new java.util.Hashtable(); public static boolean SymTlookup(Object id) { return SymTabel.containsKey((Object)id); } public static SymData SymTinsert(Object id, int type) throws ParseException { SymData sd; if (SymTabel.containsKey((Object)id)) sd=(SymData)SymTabel.get((Object)id); else { sd = new SymData(); sd.type = type; SymTabel.put((Object)id,(Object)sd); } return sd; }

  12. Symboltabel kald void StartSymbol() : {Token id=null;} { <PROGRAM> <LPAREN> id= <IDENTIFIER> <RPAREN> (statement())* <SLUT> {SymTinsert(id.image, PROG); System.out.println("Parsning slut.");} } void statement() : {Token id=null;} { id= <IDENTIFIER> <EQU> exp() <SEMICOLON> {SymTinsert(id.image, FLOAT); System.out.println("Statement.");} }

  13. Lookahead void dconstspes() : {} { LOOKAHEAD(3) <IDENTIFIER> <COLON> <TRUE> <SEMICOLON> | LOOKAHEAD(3) <IDENTIFIER> <COLON> <FALSE> <SEMICOLON> }

More Related