📄 parser.cup
字号:
non_term_name_list ::= non_term_name_list COMMA new_non_term_id | new_non_term_id ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ precedence_list ::= precedence_l | empty; /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ precedence_l ::= precedence_l preced | preced;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ preced ::= PRECEDENCE LEFT {: update_precedence(assoc.left); :} terminal_list SEMI | PRECEDENCE RIGHT {: update_precedence(assoc.right); :} terminal_list SEMI | PRECEDENCE NONASSOC {: update_precedence(assoc.nonassoc); :} terminal_list SEMI ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ terminal_list ::= terminal_list COMMA terminal_id | terminal_id ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ terminal_id ::= term_id:sym {: add_precedence(sym); RESULT = sym; :}; /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ term_id ::= symbol_id:sym {: /* check that the symbol_id is a terminal */ if (symbols.get(sym) == null) { /* issue a message */ lexer.emit_error("Terminal \"" + sym + "\" has not been declared"); } RESULT = sym; :};/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ start_spec ::= START WITH nt_id:start_name {: /* verify that the name has been declared as a non terminal */ non_terminal nt = (non_terminal)non_terms.get(start_name); if (nt == null) { lexer.emit_error( "Start non terminal \"" + start_name + "\" has not been declared"); } else { /* remember the non-terminal for later */ start_nt = nt; /* build a special start production */ new_rhs(); add_rhs_part(add_lab(new symbol_part(start_nt), "start_val")); add_rhs_part(new symbol_part(terminal.EOF)); add_rhs_part(new action_part("RESULT = start_val;")); emit.start_production = new production(non_terminal.START_nt, rhs_parts, rhs_pos); new_rhs(); } :} SEMI | empty ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ production_list ::= production_list production | production;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ production ::= nt_id:lhs_id {: /* lookup the lhs nt */ lhs_nt = (non_terminal)non_terms.get(lhs_id); /* if it wasn't declared, emit a message */ if (lhs_nt == null) { if (lexer.error_count == 0) lexer.emit_error("LHS non terminal \"" + lhs_id + "\" has not been declared"); } /* reset the rhs accumulation */ new_rhs(); :} COLON_COLON_EQUALS {: :} rhs_list SEMI | error {: lexer.emit_error("Syntax Error"); :} SEMI ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ rhs_list ::= rhs_list BAR rhs | rhs;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ rhs ::= prod_part_list PERCENT_PREC term_id:term_name {: java_cup.symbol sym = null; if (lhs_nt != null) { /* Find the precedence symbol */ if (term_name == null) { System.err.println("No terminal for contextual precedence"); sym = null; } else { sym = ((symbol_part)symbols.get(term_name)).the_symbol(); } /* build the production */ production p; if ((sym!=null) && (sym instanceof terminal)) { p = new production(lhs_nt, rhs_parts, rhs_pos, ((terminal)sym).precedence_num(), ((terminal)sym).precedence_side()); ((symbol_part)symbols.get(term_name)).the_symbol().note_use(); } else { System.err.println("Invalid terminal " + term_name + " for contextual precedence assignment"); p = new production(lhs_nt, rhs_parts, rhs_pos); } /* if we have no start non-terminal declared and this is the first production, make its lhs nt the start_nt and build a special start production for it. */ if (start_nt == null) { start_nt = lhs_nt; /* build a special start production */ new_rhs(); add_rhs_part(add_lab(new symbol_part(start_nt),"start_val")); add_rhs_part(new symbol_part(terminal.EOF)); add_rhs_part(new action_part("RESULT = start_val;")); if ((sym!=null) && (sym instanceof terminal)) { emit.start_production = new production(non_terminal.START_nt, rhs_parts, rhs_pos, ((terminal)sym).precedence_num(), ((terminal)sym).precedence_side()); } else { emit.start_production = new production(non_terminal.START_nt, rhs_parts, rhs_pos); } new_rhs(); } } /* reset the rhs accumulation in any case */ new_rhs(); :} | prod_part_list {: if (lhs_nt != null) { /* build the production */ production p = new production(lhs_nt, rhs_parts, rhs_pos); /* if we have no start non-terminal declared and this is the first production, make its lhs nt the start_nt and build a special start production for it. */ if (start_nt == null) { start_nt = lhs_nt; /* build a special start production */ new_rhs(); add_rhs_part(add_lab(new symbol_part(start_nt),"start_val")); add_rhs_part(new symbol_part(terminal.EOF)); add_rhs_part(new action_part("RESULT = start_val;")); emit.start_production = new production(non_terminal.START_nt, rhs_parts, rhs_pos); new_rhs(); } } /* reset the rhs accumulation in any case */ new_rhs(); :} ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ prod_part_list ::= prod_part_list prod_part | empty;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ prod_part ::= symbol_id:symid opt_label:labid {: /* try to look up the id */ production_part symb = (production_part)symbols.get(symid); /* if that fails, symbol is undeclared */ if (symb == null) { if (lexer.error_count == 0) lexer.emit_error("java_cup.runtime.Symbol \"" + symid + "\" has not been declared"); } else { /* add a labeled production part */ add_rhs_part(add_lab(symb, labid)); } :} | CODE_STRING:code_str {: /* add a new production part */ add_rhs_part(new action_part(code_str)); :} ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ opt_label ::= COLON label_id:labid {: RESULT = labid; :} | empty {: RESULT = null; :} ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ multipart_id ::= multipart_id DOT robust_id:another_id {: append_multipart(another_id); :} | robust_id:an_id {: append_multipart(an_id); :} ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ import_id ::= multipart_id DOT STAR {: append_multipart("*"); :} | multipart_id ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ type_id ::= multipart_id | type_id LBRACK RBRACK {: multipart_name = multipart_name.concat("[]"); :} ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ new_term_id ::= ID:term_id {: /* see if this terminal has been declared before */ if (symbols.get(term_id) != null) { /* issue a message */ lexer.emit_error("java_cup.runtime.Symbol \"" + term_id + "\" has already been declared"); } else { /* if no type declared, declare one */ if (multipart_name.equals("")) { append_multipart("Object"); } /* build a production_part and put it in the table */ symbols.put(term_id, new symbol_part(new terminal(term_id, multipart_name))); } :} ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ new_non_term_id ::= ID:non_term_id {: /* see if this non terminal has been declared before */ if (symbols.get(non_term_id) != null) { /* issue a message */ lexer.emit_error( "java_cup.runtime.Symbol \"" + non_term_id + "\" has already been declared"); } else { if (multipart_name.equals("")) { append_multipart("Object"); } /* build the non terminal object */ non_terminal this_nt = new non_terminal(non_term_id, multipart_name); /* put it in the non_terms table */ non_terms.put(non_term_id, this_nt); /* build a production_part and put it in the symbols table */ symbols.put(non_term_id, new symbol_part(this_nt)); } :} ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ nt_id ::= ID:the_id {: RESULT = the_id; :} | error {: lexer.emit_error("Illegal use of reserved word"); RESULT="ILLEGAL"; :} ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ symbol_id ::= ID:the_id {: RESULT = the_id; :} | error {: lexer.emit_error("Illegal use of reserved word"); RESULT="ILLEGAL"; :} ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ label_id ::= robust_id:the_id {: RESULT = the_id; :} ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ robust_id ::= /* all ids that aren't reserved words in Java */ ID:the_id {: RESULT = the_id; :} /* package is reserved. */ /* import is reserved. */ | CODE {: RESULT = "code"; :} | ACTION {: RESULT = "action"; :} | PARSER {: RESULT = "parser"; :} | TERMINAL {: RESULT = "terminal"; :} | NON {: RESULT = "non"; :} | NONTERMINAL {: RESULT = "nonterminal"; :} | INIT {: RESULT = "init"; :} | SCAN {: RESULT = "scan"; :} | WITH {: RESULT = "with"; :} | START {: RESULT = "start"; :} | PRECEDENCE {: RESULT = "precedence"; :} | LEFT {: RESULT = "left"; :} | RIGHT {: RESULT = "right"; :} | NONASSOC {: RESULT = "nonassoc"; :} | error {: lexer.emit_error("Illegal use of reserved word"); RESULT="ILLEGAL"; :} ;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ non_terminal ::= NON TERMINAL | NONTERMINAL;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ opt_semi ::= /* nothing */ | SEMI;/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */ empty ::= /* nothing */;/*----------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -