Search code examples
compiler-theory

Phases of a compiler?


At which phase of the compilation are keywords of a programming language recognized?

I am sort of confused between

  1. The lexical analysis.
  2. The parsing of the program.

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.


Solution

  • 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 .