Compiler Construction Week 5
Compiler Construction Week 5
Zulfiqar Ali
UIT University
Week 5 - Contents
• Syntax Definition
• Definition of Grammar
Syntax Definition
• The “Context-free grammar," or ”Grammar" for short -
that is used to specify the syntax of a language.
• A grammar naturally describes the hierarchical
structure of most programming language constructs.
• For example, an if-else statement in Java can have the
form
if ( expression ) statement else statement
if ( expression ) statement else statement
• That is, an if-else statement is the concatenation
of the keyword if, an opening parenthesis, an
expression, a closing parenthesis, a statement,
the keyword else, and another statement.
• Using the variable expr to denote an expression
and the variable stmt to denote a statement, this
structuring rule can be expressed as
stmt -> if ( expr ) stmt else stmt
stmt -> if ( expr ) stmt else stmt
• in which the arrow may be read as "can have
the form." Such a rule is called a production.
• In a production, lexical elements like the
keyword if and the parentheses are called
terminals. Variables like expr and stmt
represent sequences of terminals and are
called non terminals.
Definition of Grammar
• We specify grammars by listing their productions, with the
productions for the start symbol listed first.
• We assume that digits, signs such as < and <=, and boldface
strings such as while are terminals.
• An italicized name is a, and any non italicized name or
symbol may be assumed to be a terminal.
• For notational convenience, productions with the same non
terminal as the head can have their bodies grouped, with
the alternative bodies separated by the symbol.
Regular Grammar
• According to Noam Chomsky there are 4 types of grammar
Grammar Grammar Accepted Language Accepted Automation
Type
Type 0 Un Restricted Grammar Recursively Turing
Enumerable Language Machine
Type 1 Context Sensitive Grammar Context Sensitive Linear
Language Bounded
Automaton
Type 2 Context Free Grammar Context Free Pushdown
Language Automata
Type 3 Regular Grammar Regular Language Finite
Automata
Definition of Grammar (CFG)
• A context-free grammar has four components
– A set of terminal symbols (Tokens)
– A set of non-terminals, sometimes called \syntactic variables.“
– A set of productions composed of
• left side: a nonterminal
• arrow: ->
• right side: a sequence of terminals and non terminals
– A start symbol (first production is for the start symbol)
• Productions with the same left side can be grouped
(separated by |)
Context Free Grammar
• Context Free Grammar is formal grammar, the syntax
or structure of a formal language can be described
using context-free grammar (CFG), a type of formal
grammar.
– V - It is the collection of variables or nonterminal symbols.
– T - It is a set of terminals.
– P - It is the production rules that consist of both terminals
and non terminals.
– S - It is the Starting symbol.
Grammar
• A Grammar G can be formally described using
4 tuples G=(V, T, S, P)
– Example: G=({S,A,B), {a,b}, S, {S→AB, A→a, B→b})
• Here V={S,A,B}, T={a, b}, S=S
and P= {S→AB, A→a, B→b})
– S->AB
– ->aB
– ->ab
Derivation of Grammar
• A grammar derives strings by beginning with
the start symbol and repeatedly replacing a
nonterminal by the body of a production for
that nonterminal. The terminal strings that
can be derived from the start symbol form the
language defined by the grammar.
Example
• G=({S,A}, {a, b}, S, {S->aAb, aA->aaAb, A-E})
– S->aAb [S->aAb]
– S->aaAbb [aA->aaAb]
– S->aaaAbbb [aA->aaAb]
– S->aaabbb [A->E]
Understand with English Grammar
• Students are awesome
Students are awesome
Noun Verb Adjective
UIT Is Good
Verb Phrase
Sentence
Understand with English Grammar
Nouns Variables Constants Students
Adjectives Or Non-Terminals Or UIT
Terminals
Verb Phrase Are, is
Sentense Awesome
Good