CPSC 323: 3.2 Syntax Analysis

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/68

flashcard set

Earn XP

Description and Tags

3.1-3.3

Last updated 11:09 PM on 10/3/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

69 Terms

1
New cards

Grammar

  • The language of grammar G is the set of all sentences that can be generated by G and it is written L(G)

  • 4 tuples G=(T,N,S,R)


2
New cards

Terminals(T)

  • In programming: identifiers (id), operators (+, *), parentheses ((, )), keywords (if, else).

• Example: { id, +, *, (, ) }

  • 1 of 4 tuples of grammar

  • Written in lowercase.

    • EX: a,b,c

  • Actual characters/lexemes of the language


3
New cards

Non-Terminals(N)

  • Syntactic variables like E,T,F

  • Get replaced by terminals or non-terminals

    • EX: E = expressions, T= term, F = factor

  • Written with capital letters

  • Represent syntactic variables


4
New cards

Start Symbol(S)

  • Special non-terminal from which every derivation begins.

  • Example: Start symbol E


5
New cards

Production Rules(R)

Tell us how non-terminals can be expanded

6
New cards

Context free(CFG)

  • If each string a is a single nonterminal then the grammar is called this.

  • The language of this is called a ___ language


7
New cards

Why is grammar needed?

•Source code is just a string of characters.

•A compiler must turn that string into a structured tree it can reason about.

•Without rules, id = id + id * id could be grouped multiple ways:

•(id = (id + id) id) vs (id = id + (id id)) have different meanings.

• Why is this important?

Same input string, two meanings.

Without rules, a compiler can’t know which one to pick → ambiguity.

• Programming languages must enforce operator precedence and associativity in their grammars to avoid such confusion.

•The formal contract that say which groupings are legal and which meaning wins.

•We need a formal way to describe programming language syntax.

8
New cards

BODMAS

9
New cards

Purpose for Chomsky’s Conventions in Grammars

  • Makes grammar rules clear, general, and gives consistent notation.

  • Helps distinguish between symbols when proving theorems, writing derivations, or doing automata theory.

    • A → aB | ab

    • B → b

  • Using Chomsky’s convention:

    • A, B are non terminals (capitals).

    • a, b are terminals (lowercase).

    • aB is a terminal followed by a nonterminal.


10
New cards

Identify each term in S → AB

  • S is a nonterminal (start symbol).

  • A and B are nonterminals.

  • AB means two nonterminals concatenated


11
New cards

Ways to represent productions in CFG

• BNF (Backus–Naur Form)

• Extended BNF (EBNF)

• Chomsky Normal Form (CNF)

• Syntax Diagrams (Railroad Diagrams)

12
New cards

BNF(Backus Naur Form)

• A formal notation for context-free grammars.

• Uses angle brackets < > for nonterminals.

• Uses ::= to mean “is defined as”.

• Alternatives are separated by |.

• The symbole | means “or”

  • Compact and standardized, used in programming language specs such as Pascal and C


13
New cards

What are these an example of?

• <expr> ::= <expr> + <term> | <term>

• <term> ::= <term> * <factor> | <factor>

• <factor> ::= ( <expr> ) | id

• <expr>, <term>, <factor> → nonterminals.

• +, *, (, ) → terminals.

• id → terminal (identifier).

BNF(Backus- Naur Form)

14
New cards

Extended BNF(EBNF)

  • Adds shortcuts to BNF to avoid repetition.

  • Common notations:

    • {…} → repeat 0 or more times.

    • […] → optional.

    • (… | …) → choice between alternatives.

  • Shorter than plain BNF

  • More human readable, used in language docs like for Java


15
New cards

Example of Same Arithmetic Grammar in EBNF

16
New cards

Chomsky Normal Form(CNF)

• A restricted form of CFG useful in theory and parsing algorithms.

• Rules must be either:

• A → BC (two nonterminals), or

• A → a (a single terminal), or

• S → ε (start symbol to empty string, optional).

17
New cards

Syntax Diagrams(Railroad Diagrams)

• A graphical way to represent grammar rules.

• Looks like train tracks easy to follow visually.

• Widely used in manuals (e.g., SQL, JSON specs).

18
New cards

Syntax Diagram for (for<expr>)

  • Start → <term> → [“+” <term> repeated] → End

  • term: Non-terminal


19
New cards

Derivation

  • The process of replacing one non-terminal symbol at a time (using grammar rules) until only terminal symbols remain.

  • The result is a sentence (something the grammar recognizes, like valid code).

  • Generally: <Expr> =>* a / (c-d), where =>* means “derives in zero or more steps.”


20
New cards

Derivation if a/(c-d)

  • Step 4-5 is derivation


<ul><li><p>Step 4-5 is derivation</p></li></ul><p></p>
21
New cards

Sentential Form

  • Any string that comes up during the process of derivation

  • Can contain

    • Terminals: Symbols that appear in the final sentence, (ex: a,c,d)

    • Non-Terminals(symbols that still need

      expansion, ex:, <Expr>, <Term>)


22
New cards

Leftmost Derivation(LMD)

  • When expanding, choose the leftmost nonterminal 1st

  • Directly tied to top-down parsing(others include recursive descent, LL parsers)

  • Follows this path


23
New cards

Rightmost Derivation(RMD)

  • Choose the rightmost nonterminal 1st

  • Tied to bottom-up parsing(like LR and shift-reduce parsers)

  • Reconstructs the derivation in reverse corresponding to this type


24
New cards

Language of a CFG

  • A Context-Free Grammar (CFG) defines a language.

  • That language is the set of all strings that can be derived from the start symbol S.

  • Mathematically:

    • L(G) = {x | S → * x}

    • S = start symbol

    • x = derived string (only terminals)

    • ⇒* = “derives in zero or more steps”

  • So, if you can derive "a / (c - d)" starting from S, then it belongs to L(G)


25
New cards

Ambiguity in Context-Free Grammars (CFGs)

  • For a single input there can be multiple valid interpretations

  • A grammar is ambiguous if there exists at least one string in its language that can have two or more different parse trees.

  • One of the largest issues in compiler design


26
New cards

Issues with Ambiguity and Compiler Design

  • It leads to confusion about how to evaluate expressions.

  • Ambiguous grammars cannot be parsed reliably by deterministic parsers (LL, LR).

  • Typically, languages resolve ambiguity using Precedence and Associativity rules


27
New cards

Chomsky Hierarchy of Grammar

  • Classifies grammars into 4 types based on restrictions on production rules

  • The types define the expressive power of the language and the computational model needed


<ul><li><p>Classifies grammars into 4 types based on restrictions on production rules</p></li><li><p>The types define the expressive power of the language and the computational model needed</p></li></ul><p></p>
28
New cards

Type 0: Unrestricted Grammar

• A Type 0 grammar is the most general form of grammar in the Chomsky hierarchy.

• Production rules have the form α → β, where α and β can be any strings made of non-terminals (N) and terminals (T).

• There are no restrictions

29
New cards

Turing Machine

  • Invented by Alan Turing

  • Accepts/recognizes type 0 languages, it has the same power as the machine

  • Mathematical model of computation

  • Consists of an unlimited memory tape, a set of states or an FSM for logic control, a head for reading, moving, and writing, and transition rules to decide state changes.

  • The head moves either left or right after writing (L | R)

  • Captures class of recursively enumerable languages(Research this)

  • Same power as type 0/unrestricted grammars


30
New cards

Unlimited tape

  • Like a long strip of tape divided into cells

  • Each cell has a alphabet symbol(listed in Σ)

  • Functions as memory and input/output medium.

  • Moves left or right along tape


31
New cards

FSM for Turing Machines

  • Has a finite set of states

  • Controls what happens based on the current state and the symbol being read from the tape


32
New cards

Read/Write Head

  • Reads and writes symbols concerning the tapes

  • Moves left and write

  • Transition function


33
New cards

Transition functions for Turing machines

  • N(q, a) → (qi, b ∈ Σ)

    • Meaning: If you are in state q and see symbol a on the tape:

    • Change to state qi,

    • Write symbol b on the tape.

  • N(q, a) → (qi, {L, R})

    • After writing, the head moves either Left (L) or Right (R).


34
New cards

Type 1: Context Sensitive Grammar

  • Productions are of the form α → β such that:

    • |β| ≥ |α| (the output is at least as long as the input, so no shrinking happens).

    • α and β are strings of terminals and non-terminals.

    • At most one non-terminal is replaced at a time.

  • Unlike other types, this doesn’t allow productions that reduce string length, except for a special case allowing S → ε if ε is in the language


35
New cards

Type 2: Context Free Grammar

  • Productions are of the form: A → β where:

    • A is a single non-terminal,

    • β is a string of terminals and/or non-terminals.

  • This restriction makes CFGs more structured than Type 0 and Type 1 grammars.


36
New cards

Type 3: Regular Grammar

Productions are of the form:

  • A → β where:

  • A is a single non-terminal.

  • β is either:

    • A string of only terminals (like a, b, aba), OR

    • A terminal string followed by at most one non-terminal (like aB, bA).

  • Strict rules give this its name



37
New cards

Lexeme

  • Raw substring in source code

  • Ex: id = id + id * id → "id", "=", "+", "*"


38
New cards

Token

  • Category/class of lexemes

  • Examples:

    • "id" → IDENTIFIER

    • "=" → ASSIGN

    • "+" → PLUS

    • "*" → MULTIPLY


39
New cards

Chomsky’s Conventions in Grammars

  • Serves the purpose of consistent notation

  • Standard symbols to represent different categories in grammars

  • Consists of:

    • Terminals(T)

    • Non-terminals(N)

    • Strings of terminals(S)

    • Mixtures of terminals and non-terminals written with greek letters


40
New cards

Identify the terms in A → aB

  • A is a nonterminal (capital).

  • a is a terminal (lowercase).

  • B is a nonterminal.

  • So aB means a terminal followed by a nonterminal.


41
New cards

Identify the terms in B → ab

  • B: Nonterminals

  • a: Terminal

  • b: Terminal

  • ab: String of 2 terminals


42
New cards

Identify terms in A → αBβ

  • A: Nonterminals

  • B: Nonterminal

  • α & β: Mix of terminals/nonterminals

  • A can be rewritten into some context α, followed by B,

    followed by β


43
New cards

What type of grammar is shown in the following rules?:

  • E → E + T

  • E → T


Chonsky Normal Form(CNF) with two non-terminals

44
New cards

Why is the string a/(<Expr> - <Term>) during the derivation of a/(c-d) a sentential form?

It still has non terminals

45
New cards
<p>What type of derivation is this?</p>

What type of derivation is this?

Leftmost derivation

46
New cards
<p>What type of derivation is this?</p>

What type of derivation is this?

Rightmost derivation

47
New cards
<p>What do these different representations of the string w = a-b+c demonstrate?</p>

What do these different representations of the string w = a-b+c demonstrate?

Ambiguity in CFGs

48
New cards
<p>What is this an example of?:</p><p>Production form: α → β where both α and β are any string of N and T</p>

What is this an example of?:

Production form: α → β where both α and β are any string of N and T

Unrestricted grammar

49
New cards

What does the form γ A δ → γ α δ mean?

This is a context sensitive grammar, A can be replaced by α, but only in the context of symbols γ (before) and δ (after)

50
New cards
<p>What is the derivation of aabbcc using these rules?</p>

What is the derivation of aabbcc using these rules?

  • Start: S

  • Apply R1: S → aSBC → aSBC

  • Apply R2 (on inner S): aSBC → aabCBC

  • Apply R3: aabCBC → aabBCC

  • Apply R4: aabBCC → aabbCC

  • Apply R5: aabbCC → aabbcC

  • Apply R6: aabbcC → aabbcc

  • Final string = aabbcc

  • Language Generated

  • This grammar generates strings of the form:

    • L(G) = { anbncn∣ n ≥ 1 }

  • That means: Equal numbers of a, b, and c.

  • Examples: abc, aabbcc, aaabbbccc, etc.


51
New cards

Linear Bounded Automation(LBA)

  • Restricted turing machine

    • Limited/bounded tape

    • Only as long as the input or a constant multiple

    • Head can’t move outside this bound/ends of input


52
New cards
  • N(q,a)→ (qi,b ∈ Σ)

  • After going to state qi, overwrite the tape symbol with b

  • Move the head l or r

  • N(q,a)→ (qi,{L,R})


Transition function for Context Sensitive Languages

53
New cards
<p>With these rules what do the following productions fall under?</p><ul><li><p>S → aB → ab (String = ab)</p></li></ul><ul><li><p>S →bA → ba (String = ba)</p></li><li><p>S → aB → a(bS) →abA→aba(Can continue to form abab, etc.)</p></li><li><p>S→ bA→b(aS)→baB - bab → balanced form</p></li><li><p>Generates the grammar L(G) = { w|w has the same # of a’s and b’s}</p><ul><li><p>EX: ab, ba, abab, aabb, aaabbb</p></li></ul></li></ul><p></p>

With these rules what do the following productions fall under?

  • S → aB → ab (String = ab)

  • S →bA → ba (String = ba)

  • S → aB → a(bS) →abA→aba(Can continue to form abab, etc.)

  • S→ bA→b(aS)→baB - bab → balanced form

  • Generates the grammar L(G) = { w|w has the same # of a’s and b’s}

    • EX: ab, ba, abab, aabb, aaabbb


Context free Grammar

54
New cards
<p>For the grammar which generates strings like</p><ul><li><p>abab</p></li><li><p>baba</p></li><li><p>abababab</p></li><li><p>babaababb</p></li></ul><p>and expression L(G) = (abab | baba)* </p><p>What type of grammar and expression is this?</p><p></p>

For the grammar which generates strings like

  • abab

  • baba

  • abababab

  • babaababb

and expression L(G) = (abab | baba)*

What type of grammar and expression is this?


Type 3: Regular

55
New cards

In Chomsky’s Hierarchy of Grammars list the

Grammar type:

Grammar accepted:

Language Accepted:

Automation:

for the 1st level

  • Type-0

  • Unrestricted Grammer

  • Recursively Enumerable Language

  • Turing machine


56
New cards

In Chomsky’s Hierarchy of Grammars list the

Grammar type:

Grammar accepted:

Language Accepted:

Automation:

for the 2nd level

  • Type-1

  • Context Sensitive Grammar

  • Context Sensitive Language

  • Linear Bounded Automation


57
New cards

In Chomsky’s Hierarchy of Grammars list the

Grammar type:

Grammar accepted:

Language Accepted:

Automation:

for the 3rd level

  • Type-2

  • Context free Grammar

  • Context free Language

  • Pushdown Automation


58
New cards

In Chomsky’s Hierarchy of Grammars list the

Grammar type:

Grammar accepted:

Language Accepted:

Automation:

for the 4th level

  • Type-3

  • Regular Grammar

  • Regular Language

  • Finite State Automation


59
New cards

Push Down Automation(PDA)

  • A way to implement a context free grammar in a similar way we design finite automata for regular expression

  • A type of computational model that extends a Finite Automaton (FA) by adding a stack as extra memory.

  • It is more powerful that FSM

  • FSM has a very limited memory, but this has more

memory, more powerful

  • __ = Finite State Machine with a stack

  • A Finite Automaton (FA) can recognize regular languages.

  • This can recognize a broader class of languages called Context-Free Languages (CFLs).

  • The stack allows this to "remember" potentially unbounded information, such as matching parentheses or nested structures


60
New cards

Components of a PDA

  • Defined as a 7-tuple

  • M = (Q, Σ, Γ, δ, q₀, Z₀, F)


61
New cards

Q

  • 1 of 7 PDA tuples

  • Finite set of states


62
New cards

Σ

  • 1 of 7 PDA tuples

  • Input alphabet(symbols from the input string)


63
New cards

Γ

  • 1 of 7 PDA tuples

  • Stack alphabet (symbols you can push/pop onto stack).


64
New cards

δ

  • 1 of 7 PDA tuples

  • Transition Function


65
New cards

q0

  • 1 of 7 PDA tuples

  • Initial state


66
New cards

Z0

  • 1 of 7 PDA tuples

  • Intial Stack symbol


67
New cards

F

  • 1 of 7 PDA tuples

  • Set of accepting states


68
New cards

Transition function for a PDA

  • δ: Q × (Σ ∪ {ε}) × Γ → P(Q × Γ*)

  • This means:

    • Looks at the current state, current input symbol (or ε), and the top of the stack.

    • Based on this, it moves to a new state and can either push, pop, or replace symbols on the stack.


69
New cards

How PDAS accept input strings

  • Final State Acceptance → If the machine ends in an accepting state after reading the input.

  • Empty Stack Acceptance → If the stack becomes empty after reading the input.

  • Some definitions use one or the other; both are equivalent in power.