At which phase of the compilation are keywords of a programming language recognized?
I am sort of confused between
I once wrote a lexer in C using regular expressions but it recognised the main()
in int main(void)
also as a keyword.
On these lines I think that we have to build a parse tree to recognize keywords.
I had to build a simple compiler this year as a project for which i used Java . The recognition of keywords was made on lexical analysis. During that phase i would read input language and create a token with a type(for keywords type was variable_declaration ) and its value.I also had different types for each case like identifier , constant,multiply operation ,adding operation etc.Then passed these tokens to a queue and next into a parser which would check grammar and create a binary tree which was then used to create the output language .
Typically, the lexical analysis phase of compilation breaks the input text up into sequences of lexemes that each belongs to some particular token type that's useful in later analysis. Consequently, keywords are usually first recognized during lexical analysis in order to make parsing easier. Since parsers tend to be implemented by writing context-free grammars of tokens rather than of lexemes (that is, the category of the lexeme rather than the contents of the lexeme), it is significantly easier to build a parser when keywords are marked during lexing. For example, if I want to have a parser that treats "if" as a keyword, then I may want a rule that looks something like this in my CFG:
Statement ::= 'IF' Expr 'THEN' Expr
If I don't categorize IF
and THEN
into their own token types, then my parser couldn't write a statement like the above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With