[antlr-interest] Syntactic anti-predicates

Gerald B. Rosenberg gbr at newtechlaw.com
Mon Feb 11 19:21:30 PST 2008


Won't this do the trick?

table:
    LEFT_BRACE PIPE ws? table_format? NL
    ( ( ws? PIPE RIGHT_BRACE )=> ws? PIPE RIGHT_BRACE | table_line )+
;

At 05:45 AM 2/11/2008, Steve Bennett wrote:
>On 2/10/08, Gavin Lambert <antlr at mirality.co.nz> wrote:
> > Why not just use:
> >
> > ruleA
> >    :  (X Y Z) => ruleXYZ
> >    |  ruleNotXYZ
> >    ;
>
>Ok, I've now got a good, non-contrived example to show the problem:
>
>table:
>    LEFT_BRACE PIPE ws? table_format? NL
>    table_line*
>    ws? PIPE RIGHT_BRACE;
>
>table_line:
>    ws?
>    (
>    (PIPE PLUS) => table_caption
>    | (PIPE HYPHEN) => table_row_separator
>    | (EXCLAMATION) => table_header_cells
>    | (PIPE) => table_data_cells
>    );
>
>----
>Here, the problem is that table_line is inside a looping construct.
>Each time it loops, there can be whitespace, then a |} (end of table),
>or one of the other forms: |-, |+, |text.
>
>By the time the syntactic predicate inside fails the match, it's too
>late. I could left factor table_line:
>
>table_line:
>    (
>    (ws? PIPE PLUS) => ws? table_caption
>    | (ws? PIPE HYPHEN) => ws? table_row_separator
>    | (ws? EXCLAMATION) => ws? table_header_cells
>    | (ws? PIPE) => ws? table_data_cells
>    );
>
>But that's inelegant. My ideal solution would have this line:
>
>    table_line*
>
>replaced with something like:
>
>    (~(ws? PIPE RIGHT_BRACE) => table_line)*
>
>The only way to get around it at the moment that I can see is to
>either left factor as above, or use a semantic predicate - which I'm
>not really sure how to use with an arbitrary length rule like ws.
>
>Oh, or I can turn the loop inside out:
>
>table:
>    LEFT_BRACE PIPE ws? table_format? NL
>    ws?
>    (table_line ws?)*
>    PIPE RIGHT_BRACE;
>
>This does actually work. It's slightly disconcerting having the
>whitespace grouped with the previous line, but it's less bad than the
>other alternatives I think.
>
>Steve

----
Gerald B. Rosenberg, Esq.
NewTechLaw
260 Sheridan Ave., Suite 208
Palo Alto, CA  94306-2009

650.325.2100  (office)  /  650.703.1724  (cell)
650.325.2107  (facsimile)

www.newtechlaw.com


CONFIDENTIALITY NOTICE:  This email message (including any 
attachments) is being sent by an attorney, is for the sole use of the 
intended recipient, and may contain confidential and privileged 
information.  Any unauthorized review, use, disclosure or 
distribution is prohibited.  If you are not the intended recipient, 
please contact the sender immediately by reply email and delete all 
copies of this message and any attachments without retaining a copy.  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080211/9648c475/attachment.html 


More information about the antlr-interest mailing list