import java_cup.runtime.*; import java.io.*; /* regular languages parser for CUP. * Copyright (C) 2006 Silvano Rivoira * This program is released under the terms of the GPL; see the file * COPYING for more details. There is NO WARRANTY on this code. */ parser code {: public static Scanner s; public static int errors = 0; public static void main(String argv[]) { for (int i = 0; i < argv.length; i++) { try { System.out.println("Parsing ["+argv[i]+"]"); s = new Scanner(new FileReader(argv[i])); rlac p = new rlac(s); p.parse(); System.out.println("Number of errors = " + errors + "."); } catch (Exception e) { e.printStackTrace(System.out); System.exit(1); } } } public void report_error(String message, Object info) { if (info instanceof String){ errors++; System.err.println(" "+ errors + "==> " + info + " "+ message + "\n Parsing resumed from 2nd token before" + s.current_lexeme()+"\n"); } else { StringBuffer m = new StringBuffer("Error "); if (info instanceof java_cup.runtime.Symbol) m.append( "("+info.toString()+")" ); m.append(" : "+message); System.err.println(m); } } public void report_fatal_error(String message, Object info) { report_error(message, info); throw new RuntimeException("Fatal Syntax Error"); } :}; terminal LALPHA, RALPHA, LDEF, RDEF, LPAREN, RPAREN, LBRACK, RBRACK, COMMA; terminal EMPTY; terminal NOT, PROD, PLUS, MINUS, UNION, STAR; terminal EQ, EQUESTION, INQUESTION; terminal ILLEGAL_CHARACTER; terminal java.lang.String IDENTIFIER, INPUTSYMBOL; non terminal goal; non terminal alphabet_declaration, expression_declaration, input_symbols, regular_definitions, regular_definition; non terminal evaluations, evaluation, string, expression, prod_expression, unary_expression, symbol; non terminal regular_expression, conc_expression, closure_expression, paren_expression; start with goal; goal ::= alphabet_declaration expression_declaration evaluations | error {: parser.report_error("alphabed_declaration","MISSING"); :} expression_declaration evaluations ; alphabet_declaration ::= LALPHA input_symbols RALPHA ; input_symbols ::= INPUTSYMBOL | input_symbols COMMA INPUTSYMBOL | input_symbols error INPUTSYMBOL {: parser.report_error("','","MISSING"); :} ; expression_declaration ::= | LDEF regular_definitions RDEF ; regular_definitions ::= | regular_definitions regular_definition ; regular_definition ::= IDENTIFIER EQ expression | IDENTIFIER error {: parser.report_error("'='","MISSING"); :} expression ; evaluations ::= | evaluations evaluation ; evaluation ::= IDENTIFIER EQUESTION IDENTIFIER | IDENTIFIER INQUESTION IDENTIFIER | IDENTIFIER error {: parser.report_error("'=?' or 'in?'","MISSING"); :} IDENTIFIER | LPAREN string RPAREN INQUESTION IDENTIFIER | LPAREN RPAREN INQUESTION IDENTIFIER | string error {: parser.report_error("'(' or ')' or 'in?'","MISSING"); :} IDENTIFIER ; string ::= INPUTSYMBOL | string INPUTSYMBOL | EMPTY | string EMPTY ; expression ::= prod_expression | expression PLUS prod_expression | error {: parser.report_error("expression","WRONG"); :} PLUS prod_expression | expression MINUS prod_expression | error {: parser.report_error("expression","WRONG"); :} MINUS prod_expression ; prod_expression ::= unary_expression | prod_expression PROD unary_expression | error {: parser.report_error("prod_expression","WRONG"); :} PROD unary_expression ; unary_expression ::= LBRACK regular_expression RBRACK | LBRACK error {: parser.report_error("regular_expression","WRONG"); :} RBRACK | LBRACK expression RBRACK | NOT unary_expression ; regular_expression ::= conc_expression | EMPTY UNION conc_expression | regular_expression UNION conc_expression | error {: parser.report_error("regular_expression","WRONG"); :} UNION conc_expression ; conc_expression ::= closure_expression | conc_expression closure_expression ; closure_expression ::= symbol | paren_expression | symbol STAR | paren_expression STAR | error {: parser.report_error("closure_expression","WRONG"); :} STAR ; symbol ::= INPUTSYMBOL | IDENTIFIER ; paren_expression ::= LPAREN regular_expression RPAREN | LPAREN error {: parser.report_error("paren_expression","WRONG"); :} RPAREN ;