[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
LALR Grammar help
From: |
Michael Schidlowsky |
Subject: |
LALR Grammar help |
Date: |
Tue, 23 Aug 2005 17:32:08 -0400 |
User-agent: |
Mozilla Thunderbird 1.0.6 (Windows/20050716) |
Hello,
I am working on a LALR grammar and am relatively knew to the craft of
writing LALR grammars. The grammar is for a procedural style
programming language that looks like java, and includes an additional
block appended to the text which is where the program should start (the
equivalent of a main method, but outside the class).
I'm including the grammar file below. I'd appreciate help in two areas:
1. How can I include support for procedure calls in addition to function
calls? Right now CUP generates a parser that recognizes
a = myclass.fun(b);
but not
myclass.fun(b);
2. Are there any parts of the grammar that those of you with more
experience would recommend that I do differently? I'm not only
interested in getting this to work properly, but in the craft of grammar
writing in general, and in making this as clear as possible to the user
or the grammar.
Regards,
Michael
btw, the grammar below is for CUP but I understand the syntax to be very
close to Bison, and I am not getting any syntax problems... my concerns
are only related to Grammar construction.
// import java_cup.runtime.*;
/* Preliminaries to set up and use the scanner. */
parser code {: private CupScanner scanner; :};
init with {: scanner = new CupScanner(); scanner.init(); :};
scan with {: return scanner.next_token(); :};
/* Terminals (tokens returned by the scanner). */
terminal Integer INT;
terminal Character CHAR;
terminal String STRING;
terminal String IDENTIFIER;
terminal EOF_TOKEN;
terminal ERROR_TOKEN;
terminal WHITESPACE;
terminal ASTNode ADD_OP, SUB_OP, MUL_OP, DIV_OP, MOD;
terminal ASTNode ADDEQ_OP, SUBEQ_OP, MULEQ_OP, DIVEQ_OP;
terminal ASTNode ASSIGN_OP, NOTEQ_OP, LT_OP, GT_OP, LTEQ_OP, GTEQ_OP;
terminal ASTNode NOT_OP, BINAND_OP, LOGAND_OP, BINOR_OP;
terminal ASTNode LOGOR_OP, EQ_OP, XOR_OP;
terminal ASTNode PERIOD, COMMA, COLON, SEMICOLON, LPAREN, RPAREN;
terminal ASTNode LBRACKET, RBRACKET, LBRACE, RBRACE, QUESTION;
terminal ASTNode CASE_KEYWORD, DO_KEYWORD, ELSE_KEYWORD, END_KEYWORD;
terminal ASTNode CLASS_KEYWORD, CONST_KEYWORD, CATCH_KEYWORD,
CONTINUE_KEYWORD;
terminal ASTNode EXTENDS_KEYWORD, FOR_KEYWORD, IF_KEYWORD,
PRIVATE_KEYWORD;
terminal ASTNode PROTECTED_KEYWORD, RETURN_KEYWORD, BREAK_KEYWORD,
STRUCT_KEYWORD;
terminal ASTNode SWITCH_KEYWORD, TRY_KEYWORD, WHILE_KEYWORD;
terminal ASTNode INT_KEYWORD, FLOAT_KEYWORD, BOOL_KEYWORD,
DEFAULT_KEYWORD;
terminal ASTNode TRUE_KEYWORD, FALSE_KEYWORD, NAMESPACE_KEYWORD,
STATIC_KEYWORD;
terminal ASTNode NEW_KEYWORD, PUBLIC_KEYWORD, FINAL_KEYWORD,
VOID_KEYWORD;
/* Non terminals */
non terminal ASTNode block;
non terminal ASTNode statement_list;
non terminal ASTNode unary_expression;
non terminal ASTNode multiplicative_expression;
non terminal ASTNode additive_expression;
non terminal ASTNode relational_expression;
non terminal ASTNode equality_expression;
non terminal ASTNode and_expression;
non terminal ASTNode inclusive_or_expression;
non terminal ASTNode conditional_and_expression;
non terminal ASTNode conditional_or_expression;
non terminal ASTNode conditional_expression;
non terminal ASTNode primary_expression;
non terminal ASTNode primary_no_array_creation_expression;
non terminal ASTNode compilation_unit;
non terminal ASTNode exclusive_or_expression;
non terminal ASTNode class_declaration_list;
non terminal ASTNode class_declaration;
non terminal ASTNode class_body;
non terminal ASTNode class_member_declarations;
non terminal ASTNode class_member_declaration;
non terminal ASTNode field_declaration;
non terminal ASTNode variable_declarators;
non terminal ASTNode variable_declarator;
non terminal ASTNode type;
non terminal ASTNode value_type;
non terminal ASTNode simple_type;
non terminal ASTNode numeric_type;
non terminal ASTNode integral_type;
non terminal ASTNode namespace_name;
non terminal ASTNode type_name;
non terminal ASTNode namespace_or_type_name;
non terminal ASTNode method_declaration;
non terminal ASTNode method_header;
non terminal ASTNode modifiers;
non terminal ASTNode method_declarator;
non terminal ASTNode member_name;
non terminal ASTNode method_body;
non terminal ASTNode fixed_parameters;
non terminal ASTNode fixed_parameter;
non terminal ASTNode assignment;
non terminal assignment_operator;
non terminal ASTNode expression;
non terminal interface_type;
non terminal ASTNode statement;
non terminal ASTNode embedded_statement;
non terminal ASTNode empty_statement;
non terminal ASTNode jump_statement;
non terminal ASTNode break_statement;
non terminal ASTNode return_statement;
non terminal ASTNode expression_statement;
non terminal ASTNode statement_expression;
non terminal ASTNode selection_statement;
non terminal ASTNode if_statement;
non terminal ASTNode boolean_expression;
non terminal ASTNode switch_statement;
non terminal ASTNode switch_block;
non terminal ASTNode switch_sections;
non terminal ASTNode switch_section;
non terminal ASTNode switch_labels;
non terminal ASTNode switch_label;
non terminal ASTNode iteration_statement;
non terminal ASTNode while_statement;
non terminal ASTNode for_statement;
non terminal ASTNode for_initializer;
non terminal ASTNode for_condition;
non terminal ASTNode for_iterator;
non terminal ASTNode statement_expression_list;
non terminal ASTNode declaration_statement;
non terminal ASTNode local_variable_declaration;
non terminal ASTNode local_variable_declarators;
non terminal ASTNode local_variable_declarator;
non terminal ASTNode local_variable_initializer;
non terminal ASTNode argument_list;
non terminal ASTNode argument;
non terminal ASTNode literal;
non terminal ASTNode boolean_literal;
non terminal ASTNode integer_literal;
non terminal ASTNode simple_name;
non terminal ASTNode parenthesized_expression;
non terminal ASTNode member_access;
non terminal ASTNode predefined_type;
non terminal ASTNode invocation_expression;
non terminal ASTNode element_access;
non terminal ASTNode expression_list;
non terminal ASTNode object_creation_expression;
non terminal ASTNode reference_type;
non terminal ASTNode class_type;
non terminal ASTNode name;
non terminal ASTNode qualified_name;
non terminal ASTNode bool_type;
non terminal ASTNode constructor_declaration;
non terminal ASTNode constructor_declarator;
non terminal ASTNode constructor_body;
/* The grammar */
start with compilation_unit;
//start with assignment;
compilation_unit::=
NAMESPACE_KEYWORD class_declaration_list:classes block:b END_KEYWORD
{: RESULT = new CompilationUnitNode(classes, b); :}
;
class_declaration_list::=
class_declaration:d
{: RESULT = new ClassDeclarationListNode(d); :}
| class_declaration:d class_declaration_list:dlist
{:
RESULT = dlist;
RESULT.addChild(d);
:}
;
class_declaration::=
CLASS_KEYWORD IDENTIFIER:id class_body:body
{: RESULT = new ClassDeclarationNode(id, body); :}
| CLASS_KEYWORD IDENTIFIER:id class_body:body SEMICOLON
{: RESULT = new ClassDeclarationNode(id, body); :}
;
class_body::=
LBRACE RBRACE
{: RESULT = new ClassBodyNode(); :}
| LBRACE class_member_declarations:declarations RBRACE
{: RESULT = new ClassBodyNode(declarations); :}
;
class_member_declarations::=
class_member_declaration:declaration
{:
RESULT = new ClassMemberDeclarationsNode(declaration);
:}
| class_member_declarations:declarations
class_member_declaration:declaration
{:
RESULT = declarations;
RESULT.addChild(declaration);
:}
;
class_member_declaration::=
field_declaration:f
{: RESULT = f; :}
| method_declaration:m
{: RESULT = m; :}
| constructor_declaration:c
{: RESULT = c; :}
;
field_declaration::=
modifiers:m type:t variable_declarators:declarators SEMICOLON
{: RESULT = new FieldDeclarationNode(m,t,declarators); :}
| type:t variable_declarators:declarators SEMICOLON
{: RESULT = new FieldDeclarationNode(new
ModifiersNode(),t,declarators); :}
;
variable_declarators::=
variable_declarator:d
{: RESULT = new VariableDeclaratorsNode(d); :}
| variable_declarators:declarators COMMA
variable_declarator:declarator
{:
RESULT = declarators;
RESULT.addChild(declarator);
:}
;
variable_declarator::=
IDENTIFIER:id
{: RESULT = new VariableDeclaratorNode(id); :}
;
method_declaration::=
method_header:header method_body:body
{: RESULT = new MethodDeclarationNode(header, body); :}
;
method_header::=
modifiers:m type:t method_declarator:d
{: RESULT = new MethodHeaderNode(m, t, d); :}
| modifiers:m VOID_KEYWORD method_declarator:d
{: RESULT = new MethodHeaderNode(m, new
TypeNode(Symbols.VOID_KEYWORD), d); :}
| type:t method_declarator:d
{: RESULT = new MethodHeaderNode(t, d); :}
| VOID_KEYWORD method_declarator:d
{: RESULT = new MethodHeaderNode(new TypeNode(Symbols.VOID_KEYWORD),
d); :}
;
modifiers::=
PUBLIC_KEYWORD:keyword
{: RESULT = new ModifiersNode(Symbols.PUBLIC_KEYWORD); :}
| PRIVATE_KEYWORD:keyword
{: RESULT = new ModifiersNode(Symbols.PRIVATE_KEYWORD); :}
| STATIC_KEYWORD:keyword
{: RESULT = new ModifiersNode(Symbols.STATIC_KEYWORD); :}
| FINAL_KEYWORD:keyword
{: RESULT = new ModifiersNode(Symbols.FINAL_KEYWORD); :}
| modifiers:mods PUBLIC_KEYWORD:keyword
{:
RESULT = mods;
RESULT.addChild(new
ModifierNode(Symbols.PUBLIC_KEYWORD));
:}
| modifiers:mods PRIVATE_KEYWORD:keyword
{:
RESULT = mods;
RESULT.addChild(new
ModifierNode(Symbols.PRIVATE_KEYWORD));
:}
| modifiers:mods STATIC_KEYWORD:keyword
{:
RESULT = mods;
RESULT.addChild(new
ModifierNode(Symbols.STATIC_KEYWORD));
:}
| modifiers:mods FINAL_KEYWORD:keyword
{:
RESULT = mods;
RESULT.addChild(new
ModifierNode(Symbols.FINAL_KEYWORD));
:}
;
method_declarator::=
IDENTIFIER:id LPAREN fixed_parameters:params RPAREN
{: RESULT = new MethodDeclaratorNode(id, params); :}
| IDENTIFIER:id LPAREN RPAREN
{: RESULT = new MethodDeclaratorNode(id); :}
;
/*
// I don't think I need this...
member_name::=
IDENTIFIER:id
{: RESULT = new
| interface_type:type PERIOD IDENTIFIER:id
;
*/
interface_type::=
type_name:tname
{: RESULT = tname; :}
;
method_body::=
block:b
{: RESULT = b; :}
;
constructor_declaration::=
constructor_declarator:dec constructor_body:body
{: RESULT = new ConstructorDeclarationNode(dec, body); :}
;
constructor_declarator::=
IDENTIFIER:id LPAREN fixed_parameters:params RPAREN
{: RESULT = new ConstructorDeclaratorNode(id,params); :}
| IDENTIFIER:id LPAREN RPAREN
{: RESULT = new ConstructorDeclaratorNode(id); :}
;
constructor_body::=
block:b
{: RESULT = b; :}
;
fixed_parameters::=
fixed_parameter:p
{: RESULT = new FixedParametersNode(p); :}
| fixed_parameters:params COMMA fixed_parameter:p
{:
RESULT = params;
RESULT.addChild(p);
:}
;
fixed_parameter::=
type:t variable_declarator:dec
{:
RESULT = new FixedParameterNode(t, dec);
:}
;
type::=
simple_type:stype
{: RESULT = stype; :}
| reference_type:rtype
{: RESULT = rtype; :}
;
simple_type::=
numeric_type:ntype
{: RESULT = ntype; :}
| bool_type:btype
{: RESULT = btype; :}
;
reference_type::=
class_type:t
{: RESULT = t; :}
;
class_type::=
name:n
{: RESULT = new TypeNode(n); :}
;
name::=
simple_name:sname
{: RESULT = sname; :}
// | qualified_name:qname
// {: RESULT = qname; :}
;
//qualified_name::=
// name:n PERIOD IDENTIFIER:id
// {:
// if (n instanceof SimpleNameNode) {
// RESULT = new QualifiedNameNode();
// RESULT.addChild((SimpleNameNode) n);
// RESULT.addChild(new SimpleNameNode(id));
// }
// else { /* QualifiedNameNode */
// RESULT = (QualifiedNameNode) n;
// RESULT.addChild(new SimpleNameNode(id));
// }
// :}
// ;
bool_type::=
BOOL_KEYWORD
{: RESULT = new TypeNode(Symbols.BOOL_KEYWORD); :}
;
numeric_type::=
integral_type:t
{: RESULT = t; :}
;
integral_type::=
INT_KEYWORD
{: RESULT = new TypeNode(Symbols.INT_KEYWORD); :}
;
block::=
LBRACE statement_list:stmtlist RBRACE
{: RESULT = new BlockNode(stmtlist); :}
| LBRACE RBRACE
{: RESULT = new BlockNode(); :}
;
statement_list::=
statement:stmt
{:
RESULT = new StatementListNode(stmt);
:}
| statement_list:stmtlist statement:stmt
{:
RESULT = stmtlist;
RESULT.addChild(stmt);
:}
;
statement::=
declaration_statement:decstmt
{: RESULT = decstmt; :}
| embedded_statement:estmt
{: RESULT = estmt; :}
;
declaration_statement::=
local_variable_declaration:dec SEMICOLON
{: RESULT = dec; :}
;
local_variable_declaration::=
type:t local_variable_declarators:decl
{: RESULT = new LocalVariableDeclarationNode(t, decl); :}
;
local_variable_declarators::=
local_variable_declarator:decl
{: RESULT = new LocalVariableDeclaratorsNode(decl); :}
| local_variable_declarators:decls COMMA
local_variable_declarator:decl
{:
RESULT = decls;
RESULT.addChild(decl);
:}
;
local_variable_declarator::=
IDENTIFIER:id
{: RESULT = new LocalVariableDeclaratorNode(id); :}
| IDENTIFIER:id ASSIGN_OP local_variable_initializer:init
{: RESULT = new LocalVariableDeclaratorNode(id, init); :}
;
local_variable_initializer::=
expression:expr
{: RESULT = expr; :}
// | array_initializer
;
embedded_statement::=
block:block
{: RESULT = block; :}
| empty_statement:emptystmt
{: RESULT = emptystmt; :}
| expression_statement:exprstmt
{: RESULT = exprstmt; :}
| selection_statement:selstmt
{: RESULT = selstmt; :}
| iteration_statement:iterstmt
{: RESULT = iterstmt; :}
| jump_statement:jstmt
{: RESULT = jstmt; :}
;
empty_statement::=
SEMICOLON
{: RESULT = new EmptyStatementNode(); :}
;
selection_statement::=
if_statement:ifstmt
{: RESULT = ifstmt; :}
| switch_statement:stmt
{: RESULT = stmt; :}
;
if_statement::=
IF_KEYWORD LPAREN boolean_expression:expr RPAREN
embedded_statement:stmt
{: RESULT = new IfNode(expr, stmt); :}
// | IF_KEYWORD LPAREN boolean_expression RPAREN
embedded_statement ELSE_KEYWORD embedded_statement
;
boolean_expression::=
expression:expr
{: RESULT = expr; :}
;
switch_statement::=
SWITCH_KEYWORD LPAREN expression:expr RPAREN switch_block:block
{: RESULT = new SwitchStatementNode(expr, block); :}
;
switch_block::=
LBRACE switch_sections:sections RBRACE
{: RESULT = new SwitchBlockNode(sections); :}
| LBRACE RBRACE
{: RESULT = new SwitchBlockNode(); :}
;
switch_sections::=
switch_section:section
{: RESULT = new SwitchSectionsNode(section); :}
| switch_sections:sections switch_section:section
{:
RESULT = sections;
RESULT.addChild(section);
:}
;
switch_section::=
switch_labels:slabels statement_list:slist
{: RESULT = new SwitchSectionNode(slabels, slist); :}
;
switch_labels::=
switch_label:slabel
{: RESULT = new SwitchLabelsNode(slabel); :}
| switch_labels:slabels switch_label:slabel
{:
RESULT = slabels;
RESULT.addChild(slabel);
:}
;
switch_label::=
CASE_KEYWORD integer_literal:n COLON
{: RESULT = new SwitchLabelNode(n); :}
| DEFAULT_KEYWORD COLON
{: RESULT = new SwitchLabelNode(); :}
;
iteration_statement::=
while_statement:wstmt
{: RESULT = wstmt; :}
| for_statement:fstmt
{: RESULT = fstmt; :}
;
while_statement::=
WHILE_KEYWORD LPAREN boolean_expression:bexpr RPAREN
embedded_statement:estmt
{: RESULT = new WhileNode(bexpr, estmt); :}
;
for_statement::=
FOR_KEYWORD LPAREN for_initializer:init SEMICOLON
for_condition:cond SEMICOLON for_iterator:iter RPAREN
embedded_statement:estmt
{: RESULT = new ForStatementNode(init, cond, iter, estmt); :}
| FOR_KEYWORD LPAREN for_initializer:init SEMICOLON
for_condition:cond SEMICOLON RPAREN embedded_statement:estmt
{: RESULT = new ForStatementNode(init, cond, new ForIteratorNode(),
estmt); :}
| FOR_KEYWORD LPAREN for_initializer:init SEMICOLON
SEMICOLON for_iterator:iter RPAREN embedded_statement:estmt
{: RESULT = new ForStatementNode(init, new ForConditionNode(),
iter, estmt); :}
| FOR_KEYWORD LPAREN for_initializer:init SEMICOLON
SEMICOLON RPAREN embedded_statement:estmt
{: RESULT = new ForStatementNode(init, new ForConditionNode(), new
ForIteratorNode(), estmt); :}
| FOR_KEYWORD LPAREN SEMICOLON for_condition:cond SEMICOLON
for_iterator:iter RPAREN embedded_statement:estmt
{: RESULT = new ForStatementNode(new ForInitializerNode(), cond,
iter, estmt); :}
| FOR_KEYWORD LPAREN SEMICOLON for_condition:cond SEMICOLON
RPAREN embedded_statement:estmt
{: RESULT = new ForStatementNode(new ForInitializerNode(), cond,
new ForIteratorNode(), estmt); :}
| FOR_KEYWORD LPAREN SEMICOLON SEMICOLON for_iterator:iter
RPAREN embedded_statement:estmt
{: RESULT = new ForStatementNode(new ForInitializerNode(), new
ForConditionNode(), iter, estmt); :}
| FOR_KEYWORD LPAREN SEMICOLON SEMICOLON RPAREN
embedded_statement:estmt
{: RESULT = new ForStatementNode(new ForInitializerNode(), new
ForConditionNode(), new ForIteratorNode(), estmt); :}
;
for_initializer::=
local_variable_declaration:dec
{: RESULT = new ForInitializerNode(dec); :}
| statement_expression_list:sexprlist
{: RESULT = new ForInitializerNode(sexprlist); :}
;
for_condition::=
boolean_expression:bexpr
{: RESULT = new ForConditionNode(bexpr); :}
;
for_iterator::=
statement_expression_list:sexprlist
{: RESULT = new ForIteratorNode(sexprlist); :}
;
statement_expression_list::=
statement_expression:sexpr
{:
RESULT = new StatementExpressionListNode(sexpr);
:}
| statement_expression_list:sexprlst COMMA
statement_expression:sexpr
{:
RESULT = sexprlst;
RESULT.addChild(sexpr);
:}
;
jump_statement::=
break_statement:bstmt
{: RESULT = bstmt; :}
| return_statement:rstmt
{: RESULT = rstmt; :}
;
break_statement::=
BREAK_KEYWORD SEMICOLON
{: RESULT = new BreakNode(); :}
;
return_statement::=
RETURN_KEYWORD expression:expr SEMICOLON
{: RESULT = new ReturnNode(expr); :}
| RETURN_KEYWORD SEMICOLON
{: RESULT = new ReturnNode(); :}
;
expression_statement::=
statement_expression:expr SEMICOLON
{: RESULT = expr; :}
;
statement_expression::=
invocation_expression:iexpr
{: RESULT = iexpr; :}
| object_creation_expression:ocexpr
{: RESULT = ocexpr; :}
| assignment:ass
{: RESULT = ass; :}
;
unary_expression::=
primary_expression:pe
{: RESULT = pe; :}
// | MUL_OP unary_expression
// | ADD_OP unary_expression
// | SUB_OP unary_expression
// | NOT_OP unary_expression
;
multiplicative_expression::=
unary_expression:unaryExpr
{: RESULT = unaryExpr; :}
| multiplicative_expression:lhs MUL_OP unary_expression:rhs
{: RESULT = new MulNode(lhs,rhs); :}
| multiplicative_expression:lhs DIV_OP unary_expression:rhs
{: RESULT = new DivNode(lhs,rhs); :}
;
additive_expression::=
multiplicative_expression:mulExpr
{: RESULT = mulExpr; :}
| additive_expression:lhs ADD_OP multiplicative_expression:rhs
{: RESULT = new AddNode(lhs,rhs); :}
| additive_expression:lhs SUB_OP multiplicative_expression:rhs
{: RESULT = new SubNode(lhs,rhs); :}
;
relational_expression::=
additive_expression:addExpr
{: RESULT = addExpr; :}
| relational_expression:rexpr LT_OP additive_expression:aexpr
{: RESULT = new RelationalExpressionNode(rexpr,aexpr,Symbols.LT_OP); :}
| relational_expression:rexpr GT_OP additive_expression:aexpr
{: RESULT = new RelationalExpressionNode(rexpr,aexpr,Symbols.GT_OP); :}
| relational_expression:rexpr LTEQ_OP additive_expression:aexpr
{: RESULT = new
RelationalExpressionNode(rexpr,aexpr,Symbols.LTEQ_OP); :}
| relational_expression:rexpr GTEQ_OP additive_expression:aexpr
{: RESULT = new
RelationalExpressionNode(rexpr,aexpr,Symbols.GTEQ_OP); :}
;
equality_expression::=
relational_expression:relExpr
{: RESULT = relExpr; :}
| equality_expression:eqexpr EQ_OP relational_expression:rexpr
{: RESULT = new EqualityExpressionNode(eqexpr,rexpr,true); :}
| equality_expression:eqexpr NOTEQ_OP relational_expression:rexpr
{: RESULT = new EqualityExpressionNode(eqexpr,rexpr,false); :}
;
and_expression::=
equality_expression:eqlExpr
{: RESULT = eqlExpr; :}
| and_expression:aexpr BINAND_OP equality_expression:eqexpr
{: RESULT = new AndExpressionNode(aexpr, eqexpr); :}
;
exclusive_or_expression::=
and_expression:andExpr
{: RESULT = andExpr; :}
| exclusive_or_expression:xorexpr XOR_OP and_expression:andexpr
{: RESULT = new ExclusiveOrExpressionNode(xorexpr, andexpr); :}
;
inclusive_or_expression::=
exclusive_or_expression:exclOrExpr
{: RESULT = exclOrExpr; :}
| inclusive_or_expression:orexpr BINOR_OP
exclusive_or_expression:xorexpr
{: RESULT = new InclusiveOrExpressionNode(orexpr, xorexpr); :}
;
conditional_and_expression::=
inclusive_or_expression:inclOrExpr
{: RESULT = inclOrExpr; :}
| conditional_and_expression:cexpr LOGAND_OP
inclusive_or_expression:orexpr
{: RESULT = new ConditionalAndExpressionNode(cexpr, orexpr); :}
;
conditional_or_expression::=
conditional_and_expression:condAndExpr
{: RESULT = condAndExpr; :}
| conditional_or_expression:corexpr LOGOR_OP
conditional_and_expression:candexpr
{: RESULT = new ConditionalOrExpressionNode(corexpr, candexpr); :}
;
conditional_expression::=
conditional_or_expression:condOrExpr
{: RESULT = condOrExpr; :}
// | conditional_or_expression QUESTION expression COLON
expression
;
assignment::=
unary_expression:lhs assignment_operator expression:rhs
{: RESULT = new AssignmentNode(lhs,rhs); :}
;
assignment_operator::=
ASSIGN_OP
;
expression::=
conditional_expression:condExpr
{: RESULT = condExpr; :}
| assignment:ass
{: RESULT = ass; :}
;
argument_list::=
argument:arg
{:
RESULT = new ArgumentListNode(arg);
:}
| argument_list:arglst COMMA argument:arg
{:
RESULT = arglst;
RESULT.addChild(arg);
:}
;
argument::=
expression:expr
{: RESULT = expr; :}
;
primary_expression::=
primary_no_array_creation_expression:pne
{: RESULT = pne; :}
;
primary_no_array_creation_expression::=
literal:lit
{: RESULT = lit; :}
| simple_name:name
{: RESULT = name; :}
| invocation_expression:iexpr
{: RESULT = iexpr; :}
| parenthesized_expression:parenExpr
{: RESULT = parenExpr; :}
| member_access:memaccess
{: RESULT = memaccess; :}
| element_access:elemaccess
{: RESULT = elemaccess; :}
| object_creation_expression:ocexpr
{: RESULT = ocexpr; :}
;
literal::=
boolean_literal:boolLit
{: RESULT = boolLit; :}
| integer_literal:intLit
{: RESULT = intLit; :}
;
boolean_literal::=
TRUE_KEYWORD
{: RESULT = new BoolNode(true); :}
| FALSE_KEYWORD
{: RESULT = new BoolNode(false); :}
;
integer_literal::=
INT:n
{: RESULT = new IntNode(n.intValue()); :}
;
simple_name::=
IDENTIFIER:id
{: RESULT = new SimpleNameNode(id); :}
;
parenthesized_expression::=
LPAREN expression:expr RPAREN
{: RESULT = expr; :}
;
member_access::=
primary_expression:pexpr PERIOD IDENTIFIER:id
{: RESULT = new MemberAccessNode(pexpr, id); :}
| predefined_type:ptype PERIOD IDENTIFIER:id
{: RESULT = new MemberAccessNode(ptype, id); :}
;
predefined_type::=
BOOL_KEYWORD
{: RESULT = new TypeNode(Symbols.BOOL_KEYWORD); :}
| INT_KEYWORD
{: RESULT = new TypeNode(Symbols.INT_KEYWORD); :}
;
invocation_expression::=
primary_expression:pexpr LPAREN argument_list:arglist RPAREN
{: RESULT = new InvocationExpressionNode(pexpr, arglist); :}
| primary_expression:pexpr LPAREN RPAREN
{: RESULT = new InvocationExpressionNode(pexpr); :}
;
element_access::=
primary_no_array_creation_expression:expr LBRACKET
expression_list:explist RBRACKET
{: RESULT = new ElementAccessNode(expr, explist); :}
;
expression_list::=
expression:expr
{:
RESULT = new ExpressionListNode(expr);
:}
| expression_list:exprlist COMMA expression:expr
{:
RESULT = exprlist;
RESULT.addChild(expr);
:}
;
object_creation_expression::=
NEW_KEYWORD class_type:ctype LPAREN argument_list:arglist
RPAREN
{: RESULT = new ObjectCreationNode(ctype, arglist); :}
| NEW_KEYWORD class_type:ctype LPAREN RPAREN
{: RESULT = new ObjectCreationNode(ctype); :}
;
- LALR Grammar help,
Michael Schidlowsky <=