Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************
- ***** SECTION 1 - OPTIONS *****
- *******************************/
- options
- {
- JAVA_UNICODE_ESCAPE = true; DEBUG_PARSER=true;
- }
- /*********************************
- ***** SECTION 2 - USER CODE *****
- *********************************/
- PARSER_BEGIN(Parser)
- public class Parser
- {
- public static void main(String args[])
- {
- Parser parser;
- if (args.length == 0)
- {
- System.out.println("Reading from standard input . . .");
- parser = new Parser(System.in);
- }
- else if (args.length == 1)
- {
- try
- {
- parser = new Parser(new java.io.FileInputStream(args[0]));
- }
- catch (java.io.FileNotFoundException e)
- {
- System.err.println("File " + args[0] + " not found.");
- return;
- }
- }
- else
- {
- System.out.println("Parser: Usage is one of:");
- System.out.println(" java Parser < inputfile");
- System.out.println("OR");
- System.out.println(" java Parser inputfile");
- return;
- }
- try
- {
- parser.program();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- PARSER_END(Parser)
- /*****************************************
- ***** SECTION 3 - TOKEN DEFINITIONS *****
- *****************************************/
- TOKEN_MGR_DECLS :
- {
- static int commentNesting = 0;
- }
- SKIP : /*** Ignoring spaces/tabs/newlines ***/
- {
- " "
- | "\t"
- | "\n"
- | "\r"
- | "\f"
- }
- SKIP : /* COMMENTS */
- {
- < "--" (~["\r", "\n"]) *>
- |"/*" { commentNesting++; } : IN_COMMENT
- }
- <IN_COMMENT> SKIP :
- {
- "/*" { commentNesting++; }
- | "*/" { commentNesting--;
- if (commentNesting == 0)
- SwitchTo(DEFAULT);
- }
- | <~[]>
- }
- TOKEN : /* keywords */
- {
- < AND : "and" >
- |< BOOL : "bool" >
- |< CONST : "const" >
- |< DO : "do" >
- |< ELSE : "else">
- |< FALSE : "false">
- |< IF : "if" >
- |< INT : "int" >
- |< MAIN : "main" >
- |< NOT : "not" >
- |< OR : "or" >
- |< RETURN : "return" >
- |< THEN : "then" >
- |< TRUE : "true" >
- |< VAR : "var" >
- |< VOID : "void" >
- |< WHILE : "while" >
- |< BEGIN : "begin" >
- |< END : "end" >
- }
- TOKEN : /* operators and relations */
- {
- < SEMIC : ";" >
- | < ASSIGN : ":=" >
- | < COLON : ":" >
- | < LBR : "(" >
- | < RBR : ")" >
- | < COMMA : "," >
- | < PLUS_SIGN : "+" >
- | < MINUS_SIGN : "-" >
- | < MULT_SIGN : "*" >
- | < DIV_SIGN : "/" >
- | < EQUAL_SIGN : "=" >
- | < NOT_EQUAL_SIGN : "!=" >
- | < LESS_THAN : "<" >
- | < GREATER_THAN : ">" >
- | < LESS_EQUALS : "<=" >
- | < GREATER_EQUALS : ">=" >
- }
- TOKEN : /* Numbers */
- {
- < NUM : (<DIGIT>)+ >
- | < #DIGIT : ["0" - "9"] >
- }
- TOKEN : /* Identifiers */
- {
- < ID : (<LETTER>) (<LETTER> | <DIGIT> | "_")* >
- | < #LETTER : ["a" - "z"] | ["A" - "Z"] >
- }
- TOKEN : /* Anything not recognised so far */
- {
- < OTHER : ~[] >
- }
- /*************************************************************************************
- ***** SECTION 4 - THE GRAMMAR & PRODUCTION RULES *****
- **************************************************************************************/
- void program() : {}
- {
- ( decl() )*
- ( function() )*
- main_prog()
- }
- void decl () : {}
- {
- ( var_decl() | const_decl() )
- }
- void var_decl() : {}
- {
- <VAR> ident_list() <COLON> type() ( <COMMA> ident_list() <COLON> type() )* <SEMIC>
- }
- void const_decl() : {}
- {
- <CONST> <ID> <COLON> type() <EQUAL_SIGN> expression() ( <COMMA> <ID> <COLON> type() <EQUAL_SIGN> expression() )* <SEMIC>
- }
- void function() : {}
- {
- type() <ID> <LBR> param_list() <RBR>
- <BEGIN>
- ( decl() )*
- ( statement() <SEMIC> )*
- <RETURN> ( expression() | {} ) <SEMIC>
- <END>
- }
- void param_list() : {}
- {
- ( <ID> <COLON> type() ( <COMMA> <ID> <COLON> type() )* | {} )
- }
- void type() : {}
- {
- <INT> | <BOOL> | <VOID>
- }
- void main_prog() : {}
- {
- <MAIN>
- <BEGIN>
- decl()
- ( statement() <SEMIC> )*
- <END>
- }
- void statement() : {}
- {
- <ID> (<ASSIGN> expression() | <LBR> arg_list() <RBR>)
- //| <ID> <LBR> arg_list() <RBR> // combined with above line
- | <BEGIN> ( statement() <SEMIC> )* <END>
- | <IF> condition() <THEN> statement() [<SEMIC> <ELSE> statement()]
- | <WHILE> condition() <DO> statement()
- | {}
- }
- void expression() : {}
- {
- //<LBR> expression() <RBR>
- fragment() [ ( <PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN> ) expression() ]
- | <LBR> expression() <RBR> [ ( <PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN> ) expression() ]
- //| <ID> <LBR> arg_list() <RBR>
- }
- void fragment() : {} // possibly combine with expression
- {
- <ID> [<LBR> arg_list() <RBR>]
- | <TRUE>
- | <FALSE>
- | <NUM>
- //| ( <PLUS_SIGN> | <MINUS_SIGN> ) fragment()
- }
- void condition() : {}
- {
- <LBR> condition() <RBR> [( <AND> | <OR>) condition()]
- |<NOT> condition()
- | expression() [( <EQUAL_SIGN> | <NOT_EQUAL_SIGN> | <LESS_THAN> | <GREATER_THAN> | <LESS_EQUALS> | <GREATER_EQUALS> ) expression()] [( <AND> | <OR>) condition()]
- //| expression() ( <AND> | <OR>) condition()
- //| <ID> <LBR> arg_list() <RBR> // might need to include this in conditionPrime
- }
- void ident_list() : {}
- {
- <ID> ( <COMMA> <ID> )*
- }
- void arg_list() : {}
- {
- ( <ID> ( <COMMA> <ID> )* | {} )
- }
Advertisement
Add Comment
Please, Sign In to add comment