170 likes | 362 Views
INVISIBLE JACC. By Ronald Hathaway. So, What is it?. Invisible jacc is a parser generator implemented in java which produces lexical analyzers and parsers back in java. High Points: Supports LALR(1) as well as full & optimized LR(1), as well as detailed control of ambiguity resolution
E N D
INVISIBLE JACC By Ronald Hathaway
So, What is it? Invisible jacc is a parser generator implemented in java which produces lexical analyzers and parsers back in java. High Points: • Supports LALR(1) as well as full & optimized LR(1), as well as detailed control of ambiguity resolution • Fully object-oriented design • Not so flashy GUI • Automatic error repair • User defined goal orientation
Grammar // ----- Generator options ----- %options: %java invisible.jacc.ex2.Ex2Grammar; //Java pkg and class // ----- Terminal symbols for the grammar ----- %terminals: ';'; '('; ')'; '+'; '-'; number;
Grammar // ----- Productions for the grammar ----- %productions: Goal -> StatementList; StatementList -> /* empty */ ; StatementList -> StatementList Statement; Statement -> Expression ';'; Expression {primary} -> Primary; Expression {add} -> Expression '+' Primary; Expression {subtract} -> Expression '-' Primary; Primary {number} -> number; Primary {paren} -> '(' Expression ')';
Grammar // ----- Character categories ----- %categories: ';' = ';'; '(' = '('; ')' = ')'; '+' = '+'; '-' = '-'; decDigit = '0'..'9'; // decimal digits 0 thru 9 space = 9 | 12 | 32; // tab, form feed, and space '/' = '/'; // slash character for comments cr = 13; // carriage return lf = 10; // line feed notEol = %any - 10 - 13; // any character that isn't a eol
Grammar // ----- Tokens ----- %tokens: ';' = ';'; '(' = '('; ')' = ')'; '+' = '+'; '-' = '-'; number = decDigit+; whiteSpace = space* ('/' '/' notEol*)?; lineEnd = cr | lf | cr lf;
Grammar, Talking Points • The first section, %options, specifies options for the parser generator. • The next two sections, %terminals and %productions, are associated with the parser. These two sections define the context-free grammar that the parser recognizes. • The last two sections, %categories and %tokens, are associated with the scanner. These sections define a set of regular expressions that the scanner recognizes as input tokens.
Grammar, Talking Points • Yes, terminals must be listed one to a line. • LHS {link name} #n -> RHS precedence The link name determines which non-terminal factory is used.
%Options • %lalr1; generate an LALR(1) grammar, • %lr1; a full LR(1) grammar • %plr1; optimized LR(1) grammar • %repair • %goal • %java
Mathematical Examples Some expressions to show that parentheses work, and that addition and subtraction group from left to right. 7+11; // should be 18 7-11; // should be -4 4-5-6; // should be (4-5)-6 = -7 (4-5)-6; // should be -7 4-(5-6); // should be 5 100-(45+12)+123; // should be 166 An invalid number. 9876543210 is too big to be an int, so the compiler should issue an error message. 9876543210; // should be error
Auto Correction The parser provides automatic error repair, using a modified LM error repair algorithm. When an error is found in the source, the parser automatically determines what symbols to insert and/or delete in order to continue the parse. A “cost” can be assigned to each symbol to tune the repair algorithm. Invisible Jacc generates the error repair tables automatically from the grammar specification. Intentionally leave off the semicolon at the end of a statement. The compiler should automatically insert the semicolon and process the statement. 10+12-6 // missing semicolon, should be 16
Auto Correction This time leave off a right parenthesis. The compiler should automatically insert the parenthesis. 23-(9-5; // missing right parenthesis, should be 19 This time put in an extra right parenthesis. The compiler should simply remove it. 82-12-7); // extra right parenthesis, should be 63
The cmd line Java invisible.jacc.gen.GenMain [-v] [-o] [-g] [-s] [-p] [-j] grammar-file Example: Java invisible.jacc.gen.GenMain -o -j invisible\jacc\ex2\Ex2Grammar.jacc Options • -v Verbose mode. • -o Create output file. • -g Create generated file. • -s Create only the scanner table. Do not create a parser table. • -p Create only the parser table. Do not create a scanner table. • -j Create Java source files for the scanner and parser tables.
Lexical • %lr1 • %none • %options • %plr1 • %productions • %reduce • %repair • %shift • %terminals • %titlecase • %tokens • %any • %categories • %charsetsize • %conditions • %digit • %goal • %java • %lalr1 • %letter • %lowercase • Uses both types of Java commenting • Keywords
Errata • The Grammar specifications for Jam are build around Invisible Jacc • Where to get it: www.invisiblesoft.com/jacc • And the ever helpful FAQ (of 201 pages) http://www.invisiblesoft.com/jacc/download/ijacc.pdf