Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 513
station Avatar asked Jan 21 '23 07:01

station


2 Answers

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 .

like image 183
Giannis Avatar answered May 17 '23 11:05

Giannis


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.

like image 45
templatetypedef Avatar answered May 17 '23 09:05

templatetypedef