dabeaz / sly

Sly Lex Yacc
Other
825 stars 109 forks source link

Allowing a feature for preprocessors by appending "lookahead" and "lookaheadstack " under "self." #33

Open tanhongze opened 5 years ago

tanhongze commented 5 years ago

Hello, I found that when writing a pre-processor, new tokens could be insert before the look-ahead token. For example, using a syntax like C pre-processor (or Verilog pre-processor)

#define a b
#define B A
a B

will generate a result as "A b". It could be solved by pushing the look-ahead token back to the look-ahead stack in my parser.

#restore the look-ahead token
if self.lookahead != None:
    self.lookaheadstack.append(self.lookahead)
    self.lookahead = None 
#insert more token to parse
self.lookaheadstack.extend(macro)

And since a macro won't need the look-ahead token, the changing of next token won't disturb the parsing process. To support this feature, the look-ahead token and the look-ahead stack need be visible in other function in the parser and I simply add "self." to each "lookahead" and "lookaheadstack" in function "parse", file "yacc.py".

It can successfully pass all of the given tests with command-line "pytest ./tests/". ========================== 11 passed in 0.08 seconds =========================== By the way, I found that SLY is pretty cool. I had a dream to write a compiler when I was having my "compilers' principles" class and it really helps.