Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Computer Science and Information System

Similar presentations


Presentation on theme: "Faculty of Computer Science and Information System"— Presentation transcript:

1 Faculty of Computer Science and Information System
1

2 A Simple Syntax-Directed Translator
Chapter 1 A Simple Syntax-Directed Translator Aims: Front end of a compiler 1. Lexical analysis 2. Parsing 3. Intermediate code generation

3

4 Figure 2.1: A code fragment to be translated
Output Input Figure 2.2: A Simplified intermediate code for the program fragment in Fig. 2.1 Figure 2.1: A code fragment to be translated

5 Lexical analysis Chapter 1
- “lexical” = “pertaining to words” = {variable names, numbers, keywords, etc. } tokens. - A lexer allows a translator to handle multicharacter constructs like identifiers, which are written as sequences of characters, but are treated as units called tokens during syntax analysis; - A lexer filters out whatever separates the tokens (the so-called white-space)  lay-out characters (spaces, newlines etc.) and comments. N.B: In theory, the work that is done during lexical analysis can be made an integral part of syntax analysis.

6 Chapter 1 Example

7 Chapter 1 7

8 Chapter 1

9 Chapter 1 - Lexers are normally constructed by lexer generators, which transform human-readable specifications of tokens and white-space into efficient programs. Regular expressions

10 Syntax analysis : Parser
Chapter 1 Syntax analysis : Parser

11 Syntax analysis : Parser
Chapter 1 Syntax analysis : Parser The parser produces a syntax tree, that is further translated into three-address code. N.B: Some compilers combine parsing and intermediate-code generation into one component

12 Intermediate code Chapter 1
Two forms of intermediate code are illustrated in Fig One form, called abstract syntax trees or simply syntax trees, represents the hierarchical syntactic structure of the source program.

13 Chapter 1

14 Using “context-free grammar” in Syntax Definition
Chapter 1 Using “context-free grammar” in Syntax Definition A set of terminal symbols, sometimes referred to as "tokens." The terminals are the elementary symbols of the language defined by the grammar. A set of nonterminals, sometimes called "syntactic variables." Each nonterminal represents a set of strings of terminals, in a manner we shall describe. A set of productions, where each production consists of a nonterminal, called the head or left side of the production, an arrow, and a sequence of terminals and/or nonterminals , called the body or right side of the production. A designation of one of the nonterminals as the start symbol.

15 Chapter 1 Example: stmt expr Keywords

16

17 Derivations 9-5+2 Parse Trees

18 Chapter 1

19 Chapter 1 Ambiguity in grammar Example

20 A grammar can have more than one parse tree generating a given string of terminals. Such a grammar is said to be ambiguous To show that a grammar is ambiguous, all we need to do is find a terminal string that is the yield of more than one parse tree. Since a string with more than one parse tree usually has more than one meaning, we need to design unambiguous grammars for compiling applications, or to use ambiguous grammars with additional rules to resolve the ambiguities.

21 Associativity of Operators
By convention, 9+5+2 is equivalent to (9+5) +2 and 9-5-2 is equivalent to ( 9-5) -2. When an operand like 5 has operators to its left and right, conventions are needed for deciding which operator applies to that operand. We say that the operator + associates to the left, because an operand with plus signs on both sides of it belongs to the operator to its left. In most programming languages the four arithmetic operators, addition, subtraction, multiplication, and division are left-associative. Some common operators such as exponentiation are right-associative. As another example, the assignment operator = in C and its descendants is right associative; that is, the expression a=b=c is treated in the same way as the expression a= (b=c ) Strings like a=b=c with a right-associative operator are generated by the following 'grammar:

22

23 Example 2.6 (Construct grammar) -1
Step 1 We start with the four common arithmetic operators and a precedence table, showing the operators in order of increasing precedence. Operators may be left-associative (meaning the operations are grouped from the left), right-associative (meaning the operations are grouped from the right) or non-associative (meaning there is no defined grouping).

24 Example 2.6 (Construct grammar) -2
Step 2 We create two nonterminals expr and term for the two levels of precedence, and an extra nonterminal factor for generating basic units in expressions.

25 Example 2.6 (Construct grammar) -3
Step 3 Now consider the binary operators, * and /, that have the highest precedence. Since these operators associate to the left, the productions are similar to those for lists that associate to the left. Similarly, expr generates lists of terms separated by the additive operators

26 A grammar for arithmetic expressions

27 Example (Homework)

28 If statement - java

29


Download ppt "Faculty of Computer Science and Information System"

Similar presentations


Ads by Google