Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  A Simple Syntax Directed Translator, Computer Science and IT Engineering

A Simple Syntax Directed Translator, Computer Science and IT Engineering - Computer Science Engineering (CSE) PDF Download

A Simple Syntax-Directed Translator

This chapter is an introduction to the compiling techniques in Chapters 3 through 6 of this book. It illustrates the techniques by developing a working Java program that translates representative programming language statements into three-address code, an intermediate representation. In this chapter, the emphasis is on the front end of a compiler, in particular on lexical analysis, parsing, and intermediate code generation. Chapters7 and 8 show how to generate machine instructions from three-address code.

We start small by creating a syntax-directed translator that maps infix arithmetic expressions into postfix expressions. We then extend this translator to map code fragments as shown in Fig. 2.1 into three-address code of the form in Fig. 2.2.

The working Java translator appears in Appendix A. The use of Java is convenient, but not essential. In fact, the ideas in this chapter predate the creation of both Java and C.

Figure 2.2: Simplified intermediate code for the program fragment in Fig. 2.1


Introduction

The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called intermediate code. The synthesis phase translates the intermediate code into the target program.

Analysis is organized around the "syntax" of the language to be compiled. The syntax of a programming language describes the proper form of its pro-grams, while the semantics of the language defines what its programs mean; that is, what each program does when it executes. For specifying syntax, we present a widely used notation, called context-free grammars or BNF (for Backus-Naur Form) in Section 2.2. With the notations currently available, the semantics of a language is much more difficult to describe than the syntax. For specifying semantics, we shall therefore use informal descriptions and suggestive examples.

Besides specifying the syntax of a language, a context-free grammar can be used to help guide the translation of programs. In Section 2.3, we introduce a grammar-oriented compiling technique known assyntax-directed translation. Parsing or syntax analysis is introduced in Section 2.4.

The rest of this chapter is a quick tour through the model of a compiler front end in Fig. 2.3. We begin with the parser. For simplicity, we consider the syntax-directed translation of infix expressions to postfix form, a notation in which operators appear after their operands. For example, the postfix form of the expression 9 - 5+ 2 is 95 - 2+. Translation into postfix form is rich enough to illustrate syntax analysis, yet simple enough that the translator is shown in full in Section 2.5. The simple translator handles expressions like 9 - 5 + 2, consisting of digits separated by plus and minus signs. One reason for starting with such simple expressions is that the syntax analyzer can work directly with the individual characters for operators and operands.

A Simple Syntax Directed Translator, Computer Science and IT Engineering - Computer Science Engineering (CSE)

 

A lexical analyzer 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; for example, in the expression count 1, the identifier count is treated as a unit. The lexical analyzer in Section 2.6 allows numbers, identifiers, and "white space" (blanks, tabs, and newlines) to appear within expressions.

Next, we consider intermediate-code generation. Two forms of intermedi-ate code are illustrated in Fig. 2.4. One form, called abstract syntax trees or simply syntax trees, represents the hierarchical syntactic structure of the source program. In the model in Fig. 2.3, the parser produces a syntax tree, that is further translated into three-address code. Some compilers combine parsing and intermediate-code generation into one component.

A Simple Syntax Directed Translator, Computer Science and IT Engineering - Computer Science Engineering (CSE)

The root of the abstract syntax tree in Fig. 2.4(a) represents an entire do-while loop. The left child of the root represents the body of the loop, which consists of only the assignment i = i + 1 ; . The right child of the root repre-sents the condition a Cil < v. An implementation of syntax trees appears in Section 2.8(a).

The other common intermediate representation, shown in Fig. 2.4(b), is a sequence of "three-address" instructions; a more complete example appears in Fig. 2.2. This form of intermediate code takes its name from instructions of the form x = y op z, where op is a binary operator, y and z the are addresses for the operands, and x is the address for the result of the operation. A three-address instruction carries out at most one operation, typically a computation, a comparison, or a branch.

In Appendix A, we put the techniques in this chapter together to build a compiler front end in Java. The front end translates statements into assembly-level instructions.

The document A Simple Syntax Directed Translator, Computer Science and IT Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

Top Courses for Computer Science Engineering (CSE)

FAQs on A Simple Syntax Directed Translator, Computer Science and IT Engineering - Computer Science Engineering (CSE)

1. What is a syntax-directed translator?
Ans. A syntax-directed translator is a compiler or interpreter that processes a program by using a set of productions to recognize the program's syntax and perform actions associated with each production. It translates the source code into a target language or executes the program directly.
2. How does a syntax-directed translator work?
Ans. A syntax-directed translator works by parsing the input program based on the grammar rules defined in its productions. It uses a parsing technique, such as LL(1) or LR(1), to generate a parse tree. During the parsing process, the translator also performs actions associated with each production, which can include semantic analysis, intermediate code generation, and optimization.
3. What is the role of semantic actions in a syntax-directed translator?
Ans. Semantic actions in a syntax-directed translator are associated with productions and are used to perform tasks beyond syntax recognition. These actions can include type checking, symbol table manipulation, code generation, and error handling. They help in analyzing and processing the program's semantics while constructing the parse tree.
4. What are the advantages of using a syntax-directed translator?
Ans. Some advantages of using a syntax-directed translator include: - It provides a structured and systematic approach to parsing and translating source code. - It allows for the separation of syntax and semantics, making it easier to modify and maintain the translator. - It enables the generation of intermediate representations, which can be optimized and targeted to different platforms. - It supports modular design, allowing different phases of translation to be implemented independently. - It can facilitate error detection and recovery by incorporating error-handling mechanisms within the translator.
5. Can a syntax-directed translator handle all programming languages?
Ans. No, a syntax-directed translator cannot handle all programming languages. The translator's ability to handle a programming language depends on the expressiveness of the grammar and the features supported by the translator. Some languages may require more complex parsing techniques or additional processing beyond what a syntax-directed translator can provide. Additionally, domain-specific languages or languages with unique syntax may not be suitable for translation using a general syntax-directed approach.
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Sample Paper

,

shortcuts and tricks

,

study material

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

Semester Notes

,

ppt

,

past year papers

,

Objective type Questions

,

Free

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

practice quizzes

,

pdf

,

Summary

,

A Simple Syntax Directed Translator

,

Viva Questions

,

mock tests for examination

,

Exam

,

A Simple Syntax Directed Translator

,

A Simple Syntax Directed Translator

,

Extra Questions

,

video lectures

,

Previous Year Questions with Solutions

,

MCQs

,

Important questions

;