0% found this document useful (0 votes)
171 views6 pages

Compilers - Syntax Analysis

This document discusses syntax analysis in compiler design. It begins by defining a compiler and the different types. It then describes the two major phases of compilation: analysis and code generation. Analysis includes lexical analysis, syntax analysis, semantic analysis, and intermediate code generation. Syntax analysis is the focus. It checks the syntactic structure of code by building a parse tree using the language grammar. The grammar and an example parsing process are provided. Backtracking may be needed when multiple productions are possible.

Uploaded by

Abu Kafsha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views6 pages

Compilers - Syntax Analysis

This document discusses syntax analysis in compiler design. It begins by defining a compiler and the different types. It then describes the two major phases of compilation: analysis and code generation. Analysis includes lexical analysis, syntax analysis, semantic analysis, and intermediate code generation. Syntax analysis is the focus. It checks the syntactic structure of code by building a parse tree using the language grammar. The grammar and an example parsing process are provided. Backtracking may be needed when multiple productions are possible.

Uploaded by

Abu Kafsha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

‫جمهورية العراق‬

‫وزارة التعليــم العـــالي والبحث العـلمي‬


‫كلية بغداد للعلوم االقتصادية الجامعة‬
‫قسم علوم الحاسبات‬

‫‪Compilers – Syntax Analysis‬‬

‫تقرير مقدم الى قسم علوم الحاسبات ‪ /‬كلية بغداد للعلوم االقتصادية الجامعة‬

‫إعداد الطالب‬
‫مازن هيثم محمد‬

‫مدرس المادة‬
‫م‪.‬م‪ .‬اسراء‬

‫اسم المادة‬
‫مترجمات‬

‫‪ 2020‬م‬
1.1 Introduction

A compiler is a computer program that translates computer code written in one


programming language (the source language) into another language (the target
language). The name compiler is primarily used for programs that translate
source code from a high-level programming language to a lower level language
(e.g., assembly language, object code, or machine code) to create an executable
program. [1]

However, there are many different types of compilers. If the compiled program
can run on a computer whose CPU or operating system is different from the one
on which the compiler runs, the compiler is a cross-compiler. A bootstrap
compiler is written in the language that it intends to compile. A program that
translates from a low-level language to a higher level one is a decompiler. A
program that translates between high-level languages is usually called a source-
to-source compiler or transcompiler. A language rewriter is usually a program
that translates the form of expressions without a change of language. The term
compiler-compiler refers to tools used to create parsers that perform syntax
analysis.[1]
1.2 Phases of a Compiler

There are two major phases of compilation, which in turn have many parts.
Each of them take input from the output of the previous level and work in a
coordinated way. [2]

Analysis Phase – An intermediate representation is created from the give


source code :[2]

1- Lexical Analyzer
2- Syntax Analyzer
3- Semantic Analyzer
4- Intermediate Code Generator

Lexical analyzer divides the program into “tokens”, Syntax analyzer recognizes
“sentences” in the program using syntax of language and Semantic analyzer
checks static semantics of each construct. Intermediate Code Generator
generates “abstract” code.[1]
1.2 Introduction to Syntax Analysis in Compiler Design

When an input string (source code or a program in some language) is given to a


compiler, the compiler processes it in several phases, starting from lexical
analysis (scans the input and divides it into tokens) to target code generation.[3]

Syntax Analysis or Parsing is the second phase, i.e. after lexical analysis. It
checks the syntactical structure of the given input, i.e. whether the given input is
in the correct syntax (of the language in which the input has been written) or
not. It does so by building a data structure, called a Parse tree or Syntax tree.
The parse tree is constructed by using the pre-defined Grammar of the language
and the input string. If the given input string can be produced with the help of
the syntax tree (in the derivation process), the input string is found to be in the
correct syntax. if not, error is reported by syntax analyzer. [3]

The Grammar for a Language consists of Production rules.

Example: Suppose Production rules for the Grammar of a language are:

Now the parser attempts to construct syntax tree from this grammar for the
given input string. It uses the given production rules and applies those as needed
to generate the string. To generate string “cad” it uses the rules as shown in the
given diagram.
In the step iii above, the production rule A->bc was not a suitable one to apply
(because the string produced is “cbcd” not “cad”), here the parser needs to
backtrack, and apply the next production rule available with A which is shown
in the step iv, and the string “cad” is produced.

Thus, the given input can be produced by the given grammar, therefore the
input is correct in syntax.

But backtrack was needed to get the correct syntax tree, which is really a
complex process to implement.

There can be an easier way to solve this, which we shall see in the next article
“Concepts of FIRST and FOLLOW sets in Compiler Design”.
REFERENCES

[1] Compilers: Principles, Techniques, and Tools by Alfred V. Aho, Ravi Sethi,
Jeffrey D. Ullman - Second Edition, 2007

[2] PC Mag Staff (28 February 2019). "Encyclopedia: Definition of Compiler".


PCMag.com. Retrieved 28 February 2017.

[3] Masaru Tomita (6 December 2012). Generalized LR Parsing. Springer


Science & Business Media. ISBN 978-1-4615-4034-2.

You might also like