LangProc / langproc-2021-cw

5 stars 4 forks source link

How to get variables from the c code in parser back to lexer file? #35

Open JeffreyWong20 opened 2 years ago

JeffreyWong20 commented 2 years ago

We are currently working on typedef but we don't know what to do in term of getting back variables from C++ files which would be called at parser back to lexer. From what we knew, to let lexer recognizes some typedef types and return a type token, it needs to know the name of that correspond type first, differentiating them from normal identifier.

johnwickerson commented 2 years ago

Yes this is the "lexer hack". I suggest you have the parser maintain a list of identifiers that have been typedef'd. E.g. when it encounters typedef int foo, it adds foo to that list. The lexer also has access to that list, and each time it lexes something that looks like an identifier, it sees if it appears in that list. If it does, then it lexes it as a typeident, and if it doesn't, it lexes it as a varident. (Or whatever names you want to use.)

JeffreyWong20 commented 2 years ago

sorry, but we don't really know how to provided "symbol table access from parser to lexer". Besides the yyparse() function between parser and lexer are there any other ways they could be linked together? We tried searching online but nothing comes handy. Are there any resources which elaborates on that ? thank you.

johnwickerson commented 2 years ago

perhaps you can find a way to make that symbol table "global", so that it is accessible from all parts of your program.