lec02-parserCFG - Syntax Analyzer Syntax Analyzer creates...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
BİM454 Programming Language Concepts 1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This syntactic structure is mostly a parse tree . Syntax Analyzer is also known as parser . The syntax of a programming is described by a context-free grammar (CFG) . We will use BNF (Backus-Naur Form) notation in the description of CFGs. The syntax analyzer (parser) checks whether a given source program satisfies the rules implied by a context-free grammar or not. If it satisfies, the parser creates the parse tree of that program. Otherwise the parser gives the error messages. A context-free grammar gives a precise syntactic specification of a programming language. the design of the grammar is an initial phase of the design of a compiler. a grammar can be directly converted into a parser by some tools.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BİM454 Programming Language Concepts 2 Parser Lexical Analyzer Parser source program token get next token parse tree Parser works on a stream of tokens. The smallest item is a token.
Background image of page 2
BİM454 Programming Language Concepts 3 Parsers (cont.) We categorize the parsers into two groups: 1. Top-Down Parser the parse tree is created top to bottom, starting from the root. 2. Bottom-Up Parser the parse is created bottom to top; starting from the leaves Both top-down and bottom-up parsers scan the input from left to right (one symbol at a time). Efficient top-down and bottom-up parsers can be implemented only for sub-classes of context-free grammars. LL for top-down parsing LR for bottom-up parsing
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BİM454 Programming Language Concepts 4 Context-Free Grammars Inherently recursive structures of a programming language are defined by a context-free grammar. In a context-free grammar, we have: A finite set of terminals (in our case, this will be the set of tokens) A finite set of non-terminals (syntactic-variables) A finite set of productions rules in the following form A α where A is a non-terminal and α is a string of terminals and non-terminals (including the empty string) A start symbol (one of the non-terminal symbol) Example: E E + E | E – E | E * E | E / E | - E E ( E ) E id
Background image of page 4
BİM454 Programming Language Concepts 5 Derivations E E+E E+E derives from E we can replace E by E+E to able to do this, we have to have a production rule E E+E in our grammar. E E+E id+E id+id A sequence of replacements of non-terminal symbols is called a derivation of id+id from E. In general a derivation step is α A β αγ β if there is a production rule A →γ in our grammar where α and β are arbitrary strings of terminal and non-terminal symbols α 1 α 2 ... α n ( α n derives from α 1 or α 1 derives α n ) : derives in one step : derives in zero or more steps : derives in one or more steps * +
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
BİM454 Programming Language Concepts 6 CFG - Terminology
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/20/2009 for the course CENG PLC taught by Professor Anyone during the Fall '09 term at Anadolu University.

Page1 / 27

lec02-parserCFG - Syntax Analyzer Syntax Analyzer creates...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online