Parsing optional semicolon at statement end

后端 未结 2 2181
不思量自难忘°
不思量自难忘° 2021-02-20 16:03

I was writing a parser to parse C-like grammars.

First, it could now parse code like:

a = 1;
b = 2;

Now I want to make the semicolon at

2条回答
  •  [愿得一人]
    2021-02-20 17:01

    What you are attempting is certainly possible.

    Ruby, in fact, does exactly this, and it has a yacc parser. Newlines soft-terminate statements, semicolons are optional, and statements are automatically continued on multiple lines "if they need it".

    Communicating between the parser and lexical analyzer may be necessary, and yes, legacy yacc is LALR(1).

    I don't know exactly how Ruby does it. My guess has always been that it doesn't actually communicate (much) but rather the lexer recognizes constructs that obviously aren't finished and silently just treats newlines as spaces until the parens and brackets balance. It must also notice when lines end with binary operators or commas and eat those newlines too.

    Just a guess, but I believe this technique would work. And Ruby is open source... if you want to see exactly how Matz did it.

提交回复
热议问题