[antlr-interest] Re: Semantic predicates that aren't & hoisting

David Jung jungdl at ornl.gov
Fri Mar 11 13:14:41 PST 2005


John D. Mitchell said:
>>>>>> "David" == David Jung <jungdl at ornl.gov> writes:
> [...]
>
> Ah, the kitchen sink approach.  :-? :-)
Yep. :)
>
>
> [...]
>
>> program : expr EOF ;
>> expr : ifExpr | addExpr ;
>> ifExpr : "if" expr "then" expr ;
>> addExpr : condExpr ( (PLUS | MINUS) expr )? ;
>> condExpr: callExpr ( GTHAN expr )? ;
>> callExpr : primaryExpr ( LPAREN RPAREN )? ;
>> primaryExpr : exprList | constant | IDENT ;
>> exprList : LCURLY ( exprListOrStmt )* RCURLY ;
>> exprListOrStmt : (RCURLY)=>exprList | expr SEMI ;
>
> The syntactic predicate doesn't make sense -- it should be LCURLY, right?
> The use of RCURLY will never match the (well-formed) input and so the
> recursive match of exprList is forced to go through the *second*
> alternative (which requires the semi at the end).

You're right.  Good spotting.  However, I still get the same
result when changed to LCURLY.
So, one of us is going mad (probably me).
Perhaps I am still missing something - what first and second alternative
are you speaking of?  The whole "{if...}" is an ExprList.  Within
that the "if {a>0;} then {f();g();}" can only be an expr, can't it?
(can't be an exprList as it doesn't start with a '{', hence
it needs a trailing ';').

Here is the output for "{if {a>0;} then {f();g();} x();}" :

 > program; LA(1)=={, LA(2)==if
  > expr; LA(1)=={, LA(2)==if
   > addExpr; LA(1)=={, LA(2)==if
    > condExpr; LA(1)=={, LA(2)==if
     > callExpr; LA(1)=={, LA(2)==if
      > primaryExpr; LA(1)=={, LA(2)==if
       > exprList; LA(1)=={, LA(2)==if
        > exprListOrStmt; LA(1)==if, LA(2)=={
         > expr; LA(1)==if, LA(2)=={
          > ifExpr; LA(1)==if, LA(2)=={
           > expr; LA(1)=={, LA(2)==a
            > addExpr; LA(1)=={, LA(2)==a
             > condExpr; LA(1)=={, LA(2)==a
              > callExpr; LA(1)=={, LA(2)==a
               > primaryExpr; LA(1)=={, LA(2)==a
                > exprList; LA(1)=={, LA(2)==a
                 > exprListOrStmt; LA(1)==a, LA(2)==>
                  > expr; LA(1)==a, LA(2)==>
                   > addExpr; LA(1)==a, LA(2)==>
                    > condExpr; LA(1)==a, LA(2)==>
                     > callExpr; LA(1)==a, LA(2)==>
                      > primaryExpr; LA(1)==a, LA(2)==>
                      < primaryExpr; LA(1)==>, LA(2)==0
                     < callExpr; LA(1)==>, LA(2)==0
                     > expr; LA(1)==0, LA(2)==;
                      > addExpr; LA(1)==0, LA(2)==;
                       > condExpr; LA(1)==0, LA(2)==;
                        > callExpr; LA(1)==0, LA(2)==;
                         > primaryExpr; LA(1)==0, LA(2)==;
                          > constant; LA(1)==0, LA(2)==;
                          < constant; LA(1)==;, LA(2)==}
                         < primaryExpr; LA(1)==;, LA(2)==}
                        < callExpr; LA(1)==;, LA(2)==}
                       < condExpr; LA(1)==;, LA(2)==}
                      < addExpr; LA(1)==;, LA(2)==}
                     < expr; LA(1)==;, LA(2)==}
                    < condExpr; LA(1)==;, LA(2)==}
                   < addExpr; LA(1)==;, LA(2)==}
                  < expr; LA(1)==;, LA(2)==}
                 < exprListOrStmt; LA(1)==}, LA(2)==then
                < exprList; LA(1)==then, LA(2)=={
               < primaryExpr; LA(1)==then, LA(2)=={
              < callExpr; LA(1)==then, LA(2)=={
             < condExpr; LA(1)==then, LA(2)=={
            < addExpr; LA(1)==then, LA(2)=={
           < expr; LA(1)==then, LA(2)=={
           > expr; LA(1)=={, LA(2)==f
            > addExpr; LA(1)=={, LA(2)==f
             > condExpr; LA(1)=={, LA(2)==f
              > callExpr; LA(1)=={, LA(2)==f
               > primaryExpr; LA(1)=={, LA(2)==f
                > exprList; LA(1)=={, LA(2)==f
                 > exprListOrStmt; LA(1)==f, LA(2)==(
                  > expr; LA(1)==f, LA(2)==(
                   > addExpr; LA(1)==f, LA(2)==(
                    > condExpr; LA(1)==f, LA(2)==(
                     > callExpr; LA(1)==f, LA(2)==(
                      > primaryExpr; LA(1)==f, LA(2)==(
                      < primaryExpr; LA(1)==(, LA(2)==)
                     < callExpr; LA(1)==;, LA(2)==g
                    < condExpr; LA(1)==;, LA(2)==g
                   < addExpr; LA(1)==;, LA(2)==g
                  < expr; LA(1)==;, LA(2)==g
                 < exprListOrStmt; LA(1)==g, LA(2)==(
                 > exprListOrStmt; LA(1)==g, LA(2)==(
                  > expr; LA(1)==g, LA(2)==(
                   > addExpr; LA(1)==g, LA(2)==(
                    > condExpr; LA(1)==g, LA(2)==(
                     > callExpr; LA(1)==g, LA(2)==(
                      > primaryExpr; LA(1)==g, LA(2)==(
                      < primaryExpr; LA(1)==(, LA(2)==)
                     < callExpr; LA(1)==;, LA(2)==}
                    < condExpr; LA(1)==;, LA(2)==}
                   < addExpr; LA(1)==;, LA(2)==}
                  < expr; LA(1)==;, LA(2)==}
                 < exprListOrStmt; LA(1)==}, LA(2)==x
                < exprList; LA(1)==x, LA(2)==(
               < primaryExpr; LA(1)==x, LA(2)==(
              < callExpr; LA(1)==x, LA(2)==(
             < condExpr; LA(1)==x, LA(2)==(
            < addExpr; LA(1)==x, LA(2)==(
           < expr; LA(1)==x, LA(2)==(
          < ifExpr; LA(1)==x, LA(2)==(
         < expr; LA(1)==x, LA(2)==(
        < exprListOrStmt; LA(1)==x, LA(2)==(
       < exprList; LA(1)==x, LA(2)==(
      < primaryExpr; LA(1)==x, LA(2)==(
     < callExpr; LA(1)==x, LA(2)==(
    < condExpr; LA(1)==x, LA(2)==(
   < addExpr; LA(1)==x, LA(2)==(
  < expr; LA(1)==x, LA(2)==(
 < program; LA(1)==x, LA(2)==(
exception during compilation:
tests/test.sg:0:0: unexpected token: x

Thanks for sticking with me in sorting this out.
-David.



More information about the antlr-interest mailing list