UNIHIVE

Study Hub UNIPORT CSC 396

CSC 396 · Automata theory, Computability and Formal Languages

Parsing and pattern matching

How compilers recognise programs: lexical analysis with patterns, top-down recursive descent and LL parsing, bottom-up shift-reduce parsing.

Our notes

2 min read

The front end of a compiler

  1. Lexical analysis (scanner): groups characters into tokens (identifiers, numbers, keywords, operators) using regular expressions and finite automata. Spaces and comments are thrown away.
  2. Syntax analysis (parser): checks the token sequence against the grammar and builds a parse tree.
  3. Semantic analysis: type checking, declarations.

Example: total := total + 15 gives tokens id(total), :=, id(total), +, num(15).

Pattern matching

Patterns (regular expressions) describe tokens:

  • identifier: letter (letter | digit)*
  • number: digit digit*

The scanner takes the longest match (so "total1" is one identifier, not total then 1) and gives keywords priority over identifiers.
Pattern matching in text (searching for a word in a document) uses the same automata; algorithms such as Knuth-Morris-Pratt (1977) avoid re-reading text.

Top-down parsing

Start from S and try to derive the input, left to right.
Recursive descent: one procedure per variable. For E → T E′, E′ → + T E′ | ε, T → F T′, T′ → * F T′ | ε, F → ( E ) | id:

  • procedure E: call T; call E′.
  • procedure E′: if the next token is +, match +, call T, call E′; otherwise do nothing (ε).
  • procedure F: if the next token is (, match (, call E, match ); else match id.

It needs a grammar without left recursion and where one token of look-ahead decides each choice (LL(1)).

Bottom-up parsing

Start from the tokens and reduce them to S, finding the rightmost derivation in reverse.
Shift-reduce with a stack: shift tokens onto the stack; when the top matches a rule's right side (a handle), reduce it to the left side.
Trace id + id with E → E + T | T, T → id:

StackInputAction
id + idshift
id+ idreduce T → id
T+ idreduce E → T
E+ idshift
E +idshift
E + idreduce T → id
E + Treduce E → E + T
Eaccept

LR parsers (and tools like yacc and bison) build tables that decide when to shift and when to reduce.

Error handling

A parser should report the line and what it expected ("; expected") and recover to find more errors, for example by skipping to the next semicolon (panic mode).

Exams usually ask

  • Describe the phases of a compiler's front end.
  • Write a recursive descent parser for a small grammar.
  • Trace a shift-reduce parse.

Common mistake

Using recursive descent on a left-recursive grammar. E → E + T makes procedure E call itself immediately without reading anything, and it never stops; remove the left recursion first.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

No reading links here yet

Our team adds free books, videos and practice for each topic, and opens every link before it goes up.

All CSC 396 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.