FREE LESSON · Languages & compilers · 1 OF 4

How Compilers Parse Code: Lexers, Parsers and ASTs

Source becomes structure — Lexing, parsing, grammars, and syntax trees

A parser recovers hierarchy from a sequence.

Lexing commonly groups characters into tokens; parsing checks those tokens against a grammar and constructs a syntax tree. Precedence and associativity decide whether a+b*c means a+(b*c). The tree discards punctuation that no longer matters while preserving the nesting required for later meaning.

Compilers do not execute text directly; each phase creates a more useful representation.

Ambiguity is a language-design problem

A grammar that admits two parse trees for one token sequence leaves meaning underdetermined unless another rule resolves it. Languages define precedence, associativity, or explicit delimiters; parser generators may report conflicts. Silently accepting whichever parse happens first is not a sound semantics.

If structure changes meaning, make the structure mechanically recoverable and visible to tools.
Open this lesson in the interactive course →