Lemon grammar file for Javascript

qunying <zhu.qunying@gmail.com>
Thu, 25 Oct 2007 21:18:35 -0000

          From comp.compilers

Related articles
Lemon grammar file for Javascript zhu.qunying@gmail.com (qunying) (2007-10-25)
| List of all articles for this month |

From: qunying <zhu.qunying@gmail.com>
Newsgroups: comp.compilers
Date: Thu, 25 Oct 2007 21:18:35 -0000
Organization: Compilers Central
Keywords: parse, question

Hi,


Would the expert help me to point me the direction of solving the
conflicts of the javascript grammar file?
I got about 21 conflicts reported by lemon.


The grammar could be relax a bit to resolve the conflict as the input
is assumed to be all valid input.


Sorry for unable to attached the file as attachment.


---8< cut here ---
/* javescript lemon parser rule file */


%name JSParser


%token_prefix TK_
%start_symbol program




/* precedence rules */
%left COMMA. /* , */
/* |= ^= &= >>>= <<= >>= -= _= %= /= *= = */
%right PIPEEQ CAROTEQ AMPEREQ URSHIFTEQ LSHIFTEQ RSHIFTEQ MINUSEQ
PLUSEQ MODEQ DEVIDEEQ TIMESEQ EQ.
%right QUESTION. /* ? : */
%left PIPE2. /* || */
%left AMPER2. /* && */
%left PIPE. /* | */
%left CAROT. /* ^ */
%left AMPER. /* & */
%left NOTEQ2 EQ3 NOTEQ EQ2. /* !== === != == */
%left IN INSTACNCEOF GTEQ LTEQ GT LT. /* in instanaceof >= <= > < */
%left URSHIFT RSHIFT LSHIFT. /* >>> >> << */
%left MINUS PLUS. /* - + */
%left ASTERISK SLASH PERCENT. /* * / % */
/* ! ~ -- ++ typeof void delete */
%right EXLCAMATION TILDE MINUS2 PLUS2 TYPEOF VOID DELETE.
%left DOT LPARAN RPARAN LBRACK RBRACK. /* . ( ) [ ] */
%left ELSE.
%left NEW. /* new */


/* A.1 lexical grammer */
literal ::= NULL.
literal ::= TRUE.
literal ::= FALSE.
literal ::= NUMERICALLITERAL.
literal ::= stringLiteral.


stringLiteral ::= QUOTE QUOTE.
stringLiteral ::= QUOTE doubleStringChars QUOTE.
stringLiteral ::= APOSTROPHE APOSTROPHE.
stringLiteral ::= APOSTROPHE singleStringChars APOSTROPHE.


doubleStringChars ::= DOUBLESTRINGCHAR.
doubleStringChars ::= doubleStringChars DOUBLESTRINGCHAR.


singleStringChars ::= SINGLESTRINGCHAR.
singleStringChars ::= singleStringChars SINGLESTRINGCHAR.


/* A.3 Expressions */
primaryExpression ::= THIS.
primaryExpression ::= IDENTIFIER.
primaryExpression ::= literal.
primaryExpression ::= arrayLiteral.
primaryExpression ::= objectLiteral.
primaryExpression ::= LPARAN expression RPARAN.


arrayLiteral ::= LBRACK RBRACK.
arrayLiteral ::= LBRACK elision RBRACK.
arrayLiteral ::= LBRACK elementList RBRACK.
arrayLiteral ::= LBRACK elementList COMMA RBRACK.
arrayLiteral ::= LBRACK elementList COMMA elision RBRACK.


elementList ::= assignmentExpression.
elementList ::= elision assignmentExpression.
elementList ::= elementList COMMA assignmentExpression.
elementList ::= elementList COMMA elision assignmentExpression.


elision ::= COMMA.
elision ::= elision COMMA.


objectLiteral ::= LBRACE RBRACE.
objectLiteral ::= LBRACE propertyNameAndValueList RBRACE.


propertyNameAndValueList ::= propertyName COLON assignmentExpression.
propertyNameAndValueList ::= propertyNameAndValueList COMMA
propertyName COLON assignmentExpression.


propertyName ::= IDENTIFIER.
propertyName ::= stringLiteral.
propertyName ::= NUMERICLITERAL.


memberExpression ::= primaryExpression.
memberExpression ::= functionExpression.
memberExpression ::= memberExpression LBRACK IDENTIFIER RBRACK.
memberExpression ::= memberExpression DOT IDENTIFIER.
memberExpression ::= NEW memberExpression arguments.


newExpression ::= memberExpression.
newExpression ::= NEW newExpression.


callExpression ::= memberExpression arguments.
callExpression ::= callExpression arguments.
callExpression ::= callExpression LBRACK expression RBRACK.
callExpression ::= callExpression DOT IDENTIFIER.


arguments ::= LPARAN RPARAN.
arguments ::= LPARAN argumentList RPARAN.


argumentList ::= assignmentExpression.
argumentList ::= argumentList COMMA assignmentExpression.


leftHandSideExpression ::= newExpression.
leftHandSideExpression ::= callExpression.


postfixExpression ::= leftHandSideExpression.
postfixExpression ::= leftHandSideExpression /* no lineTerminator */
PLUS2.
postfixExpression ::= leftHandSideExpression /* no lineTerminator */
MINUS2.


unaryExpression ::= postfixExpression.
unaryExpression ::= DELETE unaryExpression.
unaryExpression ::= VOID unaryExpression.
unaryExpression ::= TYPEOF unaryExpression.
unaryExpression ::= PLUS2 unaryExpression.
unaryExpression ::= MINUS2 unaryExpression.
unaryExpression ::= PLUS unaryExpression.
unaryExpression ::= MINUS unaryExpression.
unaryExpression ::= TILDE unaryExpression.
unaryExpression ::= EXLCAMATION unaryExpression.


multiplicativeExpression ::= unaryExpression.
multiplicativeExpression ::= multiplicativeExpression ASTERISK
unaryExpression.
multiplicativeExpression ::= multiplicativeExpression DEVIDE
unaryExpression.
multiplicativeExpression ::= multiplicativeExpression PERCENT
unaryExpression.


additiveExpression ::= multiplicativeExpression.
additiveExpression ::= additiveExpression PLUS
multiplicativeExpression.
additiveExpression ::= additiveExpression MINUS
multiplicativeExpression.


shiftExpression ::= additiveExpression.
shiftExpression ::= shiftExpression LSHIFT additiveExpression.
shiftExpression ::= shiftExpression RSHIST additiveExpression.
shiftExpression ::= shiftExpression URSHIFT additiveExpression.


relationalExpressionNoIn ::= shiftExpression.
relationalExpressionNoIn ::= relationalExpressionNoIn LT
shiftExpression.
relationalExpressionNoIn ::= relationalExpressionNoIn GT
shiftExpression.
relationalExpressionNoIn ::= relationalExpressionNoIn LTEQ
shiftExpression.
relationalExpressionNoIn ::= relationalExpressionNoIn GTEQ
shiftExpression.
relationalExpressionNoIn ::= relationalExpressionNoIn INSTANCEOF
shiftExpression.


relationalExpression ::= relationalExpressionNoIn IN shiftExpression.
relationalExpression ::= relationalExpression IN shiftExpression.


equalityExpression ::= relationalExpression.
equalityExpression ::= equalityExpression EQ2 relationalExpression.
equalityExpression ::= equalityExpression NOTEQ relationalExpression.
equalityExpression ::= equalityExpression EQ3 relationalExpression.
equalityExpression ::= equalityExpression NOTEQ2 relationalExpression.




equalityExpressionNoIn ::= relationalExpressionNoIn.
equalityExpressionNoIn ::= equalityExpressionNoIn EQ2
relationalExpressionNoIn.
equalityExpressionNoIn ::= equalityExpressionNoIn NOTEQ
relationalExpressionNoIn.
equalityExpressionNoIn ::= equalityExpressionNoIn EQ3
relationalExpressionNoIn.
equalityExpressionNoIn ::= equalityExpressionNoIn NOTEQ2
relationalExpressionNoIn.


bitwiseAndExpression ::= equalityExpression.
bitwiseAndExpression ::= bitwiseAndExpression AMPER
equalityExpression.


bitwiseAndExpressionNoIn ::= equalityExpressionNoIn.
bitwiseAndExpressionNoIn ::= bitwiseAndExpressionNoIn AMPER
equalityExpressionNoIn.


bitwiseXorExpression ::= bitwiseAndExpression.
bitwiseXorExpression ::= bitwiseXorExpression CAROT
bitwiseAndExpression.


bitwiseXorExpressionNoIn ::= bitwiseAndExpressionNoIn.
bitwiseXorExpressionNoIn ::= bitwiseXorExpressionNoIn CAROT
bitwiseAndExpressionNoIn.


bitwiseOrExpression ::= bitwiseXorExpression.
bitwiseOrExpression ::= bitwiseOrExpression PIPE bitwiseXorExpression.




bitwiseOrExpressionNoIn ::= bitwiseXorExpressionNoIn.
bitwiseOrExpressionNoIn ::= bitwiseOrExpressionNoIn PIPE
bitwiseXorExpressionNoIn.


logicalAndExpression ::= bitwiseOrExpression.
logicalAndExpression ::= logicalAndExpression AMPER2
bitwiseOrExpression.


logicalAndExpressionNoIn ::= bitwiseOrExpressionNoIn.
logicalAndExpressionNoIn ::= logicalAndExpressionNoIn AMPER2
bitwiseOrExpressionNoIn.


logicalOrExpression ::= logicalAndExpression.
logicalOrExpression ::= logicalOrExpression PIPE2
logicalAndExpression.




logicalOrExpressionNoIn ::= logicalAndExpressionNoIn.
logicalOrExpressionNoIn ::= logicalOrExpression PIPE2
logicalAndExpressionNoIn.


conditionalExpression ::= logicalOrExpression.
conditionalExpression ::= logicalOrExpression QUESTION
assignmentExpression COLON assignmentExpression.


conditionalExpressionNoIn ::= logicalOrExpressionNoIn.
conditionalExpressionNoIn ::= logicalOrExpressionNoIn QUESTION
assignmentExpressionNoIn COLON assignmentExpressionNoIn.


assignmentExpression ::= conditionalExpression.
assignmentExpression ::= leftHandSideExpression assignmentOperator
assignmentExpression.


assignmentExpressionNoIn ::= conditionalExpressionNoIn.
assignmentExpressionNoIn ::= leftHandSideExpression assignmentOperator
assignmentExpressionNoIn.


assignmentOperator ::= EQ.
assignmentOperator ::= TIMESEQ.
assignmentOperator ::= DEVIDEEQ.
assignmentOperator ::= MODEQ.
assignmentOperator ::= PLUSEQ.
assignmentOperator ::= MINUSEQ.
assignmentOperator ::= LSHIFTEQ.
assignmentOperator ::= RSHIFTEQ.
assignmentOperator ::= URSHIFTEQ.
assignmentOperator ::= AMPEREQ.
assignmentOperator ::= CAROTEQ.
assignmentOperator ::= PIPEEQ.


expression ::= assignmentExpression.
expression ::= expression COMMA assignmentExpression.


expressionNoIn ::= assignmentExpressionNoIn.
expressionNoIn ::= expressionNoIn COMMA assignmentExpressionNoIn.


/* A.4 Statements */
statement ::= block.
statement ::= variableStatement.
statement ::= emptyStatement.
statement ::= expressionStatement.
statement ::= ifStatement.
statement ::= iterationStatement.
statement ::= continueStatement.
statement ::= breakStatement.
statement ::= returnStatement.
statement ::= withStatement.
statement ::= labelledStatement.
statement ::= switchStatement.
statement ::= throwStatement.
statement ::= tryStatement.


block ::= objectLiteral.
block ::= LBRACE statementList RBRACE.


statementList ::= statement.
statementList ::= statementList statement.


variableStatement ::= VAR variableDeclarationList SEMI.
variableDeclarationList ::= variableDeclaration.
variableDeclarationList ::= variableDeclarationList COMMA
variableDeclaration.


variableDeclarationListNoIn ::= variableDeclarationNoIn.
variableDeclarationListNoIn ::= variableDeclarationListNoIn COMMA
variableDeclarationNoIn.


variableDeclaration ::= IDENTIFIER.
variableDeclaration ::= IDENTIFIER initialiser.


variableDeclarationNoIn ::= IDENTIFIER.
variableDeclarationNoIn ::= IDENTIFIER initialiserNoIn.


initialiser ::= EQ assignmentExpression.


initialiserNoIn ::= EQ assignmentExpressionNoIn.


emptyStatement ::= SEMI.


expressionStatement ::= /* lookahead not a member of [{, function] */
expression SEMI.


ifStatement ::= IF LPARAN expression RPARAN statement.
ifStatement ::= IF LPARAN expression RPARAN statement ELSE statement.


iterationStatement ::= DO statement WHILE LPARAN expression RPARAN
SEMI.
iterationStatement ::= WHILE LPARAN expression RPARAN statement.
iterationStatement ::= FOR LPARAN SEMI SEMI SEMI RPARAN statement.
iterationStatement ::= FOR LPARAN expressionNoIn SEMI SEMI SEMI RPARAN
statement.
iterationStatement ::= FOR LPARAN expressionNoIn SEMI expression SEMI
SEMI RPARAN statement.
iterationStatement ::= FOR LPARAN expressionNoIn SEMI SEMI expression
SEMI RPARAN statement.
iterationStatement ::= FOR LPARAN expressionNoIn SEMI expression SEMI
expression SEMI RPARAN statement.
iterationStatement ::= FOR LPARAN SEMI expression SEMI SEMI RPARAN
statement.
iterationStatement ::= FOR LPARAN SEMI SEMI expression SEMI RPARAN
statement.
iterationStatement ::= FOR LPARAN SEMI expression SEMI expression SEMI
RPARAN statement.
iterationStatement ::= FOR LPARAN VAR variableDeclarationListNoIn SEMI
SEMI SEMI RPARAN statement.
iterationStatement ::= FOR LPARAN VAR variableDeclarationListNoIn SEMI
expression SEMI SEMI RPARAN statement.
iterationStatement ::= FOR LPARAN VAR variableDeclarationListNoIn SEMI
expression SEMI expression SEMI RPARAN statement.
iterationStatement ::= FOR LPARAN VAR variableDeclarationListNoIn SEMI
SEMI expression SEMI RPARAN statement.
iterationStatement ::= FOR LPARAN leftHandSideExpression IN expression
RPARAN statement.
iterationStatement ::= FOR LPARAN VAR variableDeclarationNoIn IN
expression RPARAN statement.


continueStatement ::= CONTINUE /* no lineTerminator here */ SEMI.
continueStatement ::= CONTINUE /* no lineTerminator here */ IDENTIFIER
SEMI.


breakStatement ::= BREAK /* no lineTerminator here */ SEMI.
breakStatement ::= BREAK /* no lineTerminator here */ IDENTIFIER SEMI.


returnStatement ::= RETURN /* no lineTerminator here */ SEMI.
returnStatement ::= RETURN /* no lineTerminator here */ expression
SEMI.


withStatement ::= WITH LPARAN expression RPARAN statement.


switchStatement ::= SWITCH LPARAN expression RPARAN caseBlock.


caseBlock ::= LBRACE RBRACE.
caseBlock ::= LBRACE caseClauses RBRACE.
caseBlock ::= LBRACE caseClauses defaultClause RBRACE.
caseBlock ::= LBRACE caseClauses defaultClause caseClauses RBRACE.
caseBlock ::= LBRACE defaultClause caseClauses RBRACE.
caseBlock ::= LBRACE defaultClause RBRACE.


caseClauses ::= caseClause.
caseClauses ::= caseClauses caseClause.


caseClause ::= CASE expression COLON.
caseClause ::= CASE expression COLON statementList.


defaultClause ::= DEFAULT COLON.
defaultClause ::= DEFAULT COLON statementList.


labelledStatement ::= IDENTIFIER COLON statement.


throwStatement ::= THROW /* no lineTerminator here */ expression SEMI.


tryStatement ::= TRY block catch_block.
tryStatement ::= TRY block finally_block.
tryStatement ::= TRY block catch_block finally_block.


catch_block ::= CATCH LPARAN IDENTIFIER RPARAN block.
finally_block ::= FINALLY block.


/* A.5 functions and programs */
functionDeclaration ::= FUNCTION IDENTIFIER LPARAN RPARAN LBRACE
functionBody RBRACE.
functionDeclaration ::= FUNCTION IDENTIFIER LPARAN formalParameterList
RPARAN LBRACE functionBody RBRACE.


functionExpression ::= FUNCTION LPARAN RPARAN LBRACE functionBody
RBRACE.
functionExpression ::= FUNCTION LPARAN formalParameterList RPARAN
LBRACE functionBody RBRACE.
functionExpression ::= FUNCTION IDENTIFIER LPARAN RPARAN LBRACE
functionBody RBRACE.
functionExpression ::= FUNCTION IDENTIFIER LPARAN formalParameterList
RPARAN LBRACE functionBody RBRACE.


formalParameterList ::= IDENTIFIER.
formalParameterList ::= formalParameterList COMMA IDENTIFIER.


functionBody ::= sourceElements.


program ::= sourceElements.


sourceElements ::= sourceElement.
sourceElements ::= sourceElements sourceElement.


sourceElement ::= statement.
sourceElement ::= functionDeclaration.


----- >8 ------



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.