1 / 13

Basic Compiler Functions Grammars Lexical Analysis Syntactic Analysis Code Generation

Basic Compiler Functions Grammars Lexical Analysis Syntactic Analysis Code Generation. High-Level Programming Language. A high-level programming language is described in terms of a grammar, which specifies the syntax of legal statements. An assignment statement:

wynn
Download Presentation

Basic Compiler Functions Grammars Lexical Analysis Syntactic Analysis Code Generation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Compiler FunctionsGrammarsLexical AnalysisSyntactic AnalysisCode Generation

  2. High-Level Programming Language • A high-level programming language is described in terms of a grammar, which specifies the syntax of legal statements. • An assignment statement: • a variable name + an assignment operator + an expression

  3. Compiler • Compilation: matching statements (written by programmers) to structures (defined by the grammar) and generating the appropriate object code • Lexical analysis (scanning) • Scanning the source statement, recognizing and classifying the various tokens, including keywords, variable names, data types, operators, etc. • Syntactic analysis (parsing) • Recognizing each statement as some language construct described by the grammar • Semantics (code generation) • Generation of the object code

  4. Grammars • A grammar is a formal description of the syntax. • BNF (Backus-Naur Form): • A simple and widely used notations for writing grammars introduced by John Backus and Peter Naur in about 1960. • Meta-symbols of BNF: • ::= "is defined as" • | "or" • < > angle brackets used to surround non-terminal symbols • A BNF rule defining a nonterminal has the form: nonterminal ::= sequence_of_alternatives consisting of strings of terminals (tokens) or nonterminals separated by the meta-symbol |

  5. Simplified Pascal Grammar Recursive rule

  6. Parse Tree(Syntax Tree) READ(VALUE) VARIANCE:=SUMSQ DIV 100 – MEAN*MEAN The multiplication and division precede the addition and subtraction

  7. Parse Tree

  8. Parse Tree

  9. Lexical Analysis • Tokens might be defined by grammar rules to be recognized by the parser: • For better efficiency, a scanner can be used instead to recognize and output the tokens in a sequence represented by fixed-length codes and the associated token specifiers.

  10. Lexical Scan

  11. Modeling Scanners as Finite Automata • Tokens can often be recognized by a finite automaton, which consists of • A finite set of states (including a starting state and one or more final states) • A set of transtitions from one state to another

  12. Finite Automata for Typical Tokens

  13. Token Recognition Algorithm

More Related