[antlr-interest] Parser --> TreeParser: help with bigger example please?

Tim Clark timgclark at gmail.com
Thu May 17 12:01:43 PDT 2007


Hi All
The book gives a rather simple (and easy to understand) example of the step
from the parser to the tree parser (for expressions). When it's a lot more
complicated (as below, for example) I'm a little stumped as to how to do it.
Any words of advice would be greatly appreciated. Intuitively it's clear
that one ends up with lists of simple things like ^('+' arg1 arg2) or ^('-'
arg), but it's a bit mind-boggling when the rules are so much more
complicated.

expr
    :
    assignExpr
    ;

assignExpr
    :
    condExpr (assignOp^ assignExpr)?
    ;

assignOp
    :
    ASSIGN | PLUS_ASSIGN | MINUS_ASSIGN | STAR_ASSIGN | DIV_ASSIGN
    | MOD_ASSIGN | CAT_ASSIGN | OR_ASSIGN | AND_ASSIGN | XOR_ASSIGN |
LCAT_ASSIGN
    ;

condExpr
    :
    condOrExpr (QUESTION^ expr COLON expr)?
    ;

condOrExpr
    :
    condAndExpr (LOGICAL_OR^ condAndExpr)*
    ;

condAndExpr
    :
    eqExpr (LOGICAL_AND^ eqExpr)*
    ;

eqExpr
    :
    relExpr ((IS_EQUAL^ | NOT_EQUAL^) relExpr)*
    ;

relExpr
    :
    incOrExpr ((LESS_THAN^ | GREATER_THAN^ | LESS_OR_EQUAL^ |
GREATER_OR_EQUAL^) incOrExpr)*
    ;

incOrExpr
    :
    excOrExpr (LINE^ excOrExpr)*
    ;

excOrExpr
    :
    andExpr (HAT^ andExpr)*
    ;

andExpr
    :
    shiftExpr (AMPER^ shiftExpr)*
    ;

shiftExpr
    :
    addExpr ((LSHIFT^ | RSHIFT^) addExpr)*
    ;

addExpr
    :
    multExpr ((PLUS^ | MINUS^ | CAT^ | LIST_CAT^) multExpr)*
    ;

multExpr
    :
    unaryExpr ((STAR^ | DIV^ | MOD^) unaryExpr)*
    ;

unaryExpr
    :
    primaryExpr
    |
    negation
    |
    binaryComp
    |
    logicalNot
    |
    sizeOf
    ;

negation
    :
    NEGATE unaryExpr
    -> ^(NEG unaryExpr)
    ;

binaryComp
    :
    COMPLEMENT unaryExpr
    -> ^(COMPL unaryExpr)
    ;

logicalNot
    :
    LOGICAL_NOT unaryExpr
    -> ^(NOT unaryExpr)
    ;

sizeOf
    :
    SIZE unaryExpr
    -> ^(SIZEOF unaryExpr)
    ;

primaryExpr
options {backtrack=true;}
    :
    constant
    |
    ID
    |
    parenExpr
    |
    funcCall
    |
    listSelector
    |
    listCons
    |
    mapCons
    |
    preInc
    |
    postInc
    |
    preDec
    |
    postDec
    ;

parenExpr
    :
    LPAREN! expr RPAREN!
    ;

exprList
    :
    expr (COMMA! expr)*
    |
    // Empty
    ;

funcCall
    :
    ID LPAREN exprList RPAREN
    -> ^(CALL ID exprList)
    ;

listCons
    :
    LBRAK exprList RBRAK
    -> ^(LIST_CONS exprList)
    ;

listSelector
    :
    ID sliceList
    -> ^(LIST_SEL ID sliceList)
    ;

sliceList
    :
    listSlice (listSlice)*
    ;

listSlice
    :
    LBRAK! expr RBRAK!
    ;

mapCons
    :
    '{{' keyValueList '}}'
    -> ^(MAP_CONS keyValueList)
    ;

keyValueList
    :
    keyValuePair (','! keyValuePair)*
    |
    /*Empty*/
    ;

keyValuePair
    :
    e1=expr ':' e2=expr
    -> ^(KEY_VALUE_PAIR $e1 $e2)
    ;

preInc
    :
    (DPLUS listSelector) => DPLUS listSelector
    -> ^(PRE_INC listSelector)
    |
    DPLUS ID
    -> ^(PRE_INC ID)
    ;

postInc
    :
    (listSelector DPLUS) => listSelector DPLUS
    -> ^(POST_INC listSelector)
    |
    ID DPLUS
    -> ^(POST_INC ID)
    ;

preDec
    :
    (DMINUS listSelector) => DMINUS listSelector
    -> ^(PRE_DEC listSelector)
    |
    DMINUS ID
    -> ^(PRE_DEC ID)
    ;

postDec
    :
    (listSelector DMINUS) => listSelector DMINUS
    -> ^(POST_DEC listSelector)
    |
    ID DMINUS
    -> ^(POST_DEC ID)
    ;

constant
    :
    STRING
    |
    NUM_INT
    |
    NUM_FLOAT
    |
    'true'
    |
    'false'
    |
    'nil'
    ;

Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070517/f8f452e5/attachment.html 


More information about the antlr-interest mailing list