Skip to content

Conversation

tanhongze
Copy link

@tanhongze tanhongze commented Apr 8, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant