goccmack / gocc

Parser / Scanner Generator
Other
622 stars 48 forks source link

How best to parse this? #131

Closed suntong closed 2 years ago

suntong commented 2 years ago

Is it possible to parse this as-is?

<expr> ::= <term> "+" <expr>
        |  <term>

<term> ::= <factor> "*" <term>
        |  <factor>

<factor> ::= "(" <expr> ")"
          |  <const>

<const> ::= integer

I'm not asking people to write a BNF parser for me but the question is on the \n. I.e., if I put it into

!whitespace : ' ' | '\t' | '\n' | '\r' ;

then how can I reliably tell that one rule is finished and time to start a new one?

What are the best approaches to parse this? thx!

suntong commented 2 years ago

well, found that problem still holds.

awalterschulze commented 2 years ago

The BNF was written for a single expr

If you want multiple, then you need to add another rule, something like:

<exprs> ::= <expr> ";" <exprs>
                  |  <expr>

You can also probably use newline instead of ; if you remove the \n from whitespace.

suntong commented 2 years ago

Ok, thanks for helping!