Parser

A parser does these following works

  1. Obtains the string of token from lexical analyzer.
  2. Verifies the token name using grammar for the source language.
  3. Reporting syntax error and recovering it.
  4. Making parse tree which will be used by other parts of the compiler. But sometimes it will not construct parse tree, it will directly match with the type and check if it is right or not.
  5. It will try to reduce the token.

Types of Parse Tree

1. Universal

This method creates all possible parse trees for a given grammar and sentence, then filters out invalid ones. It's the most comprehensive but least efficient.

2. Top Down

Starts from the start symbol (S) and expands downward using production rules until reaching the input symbols. Like solving a puzzle from the big picture to details.

3. Bottom up

Starts with input tokens and gradually reduces them to higher-level constructs until reaching the start symbol. Like building a pyramid from the base up.

Syntax Error Handling

Two of these strategies, called panic-mode and phrase-level recovery, are discussed in more detail in connection with specific parsing methods.

Panic Mode Recovery

  1. Simple and aggressive
  2. Discards tokens until reaching a known safe. Ex - Panic mode would skip everything until semicolon in a code. Result: Skips entire statement, continues with next statement
  3. More likely to skip larger portions of code