Sei sulla pagina 1di 16

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

BELGAUM

(SYNTAX ANALYSER FOR IF ELSE


STATEMENT IN C)

PROJECT WORK
Of
5TH SEMESTER COMPUTER SCIENCE & ENGINEERING
NAME ROLL.NO USN
ARJUN M.R 10 4JC07CS014
ESHWARA .C 19 4JC07CS032
HARIPRASAD K.B 21 4JC07CS038
Submitted by

Under the guidance of


Smt.Trishiladevi C Nagavi
Lecturer
Dept of CS&E, SJCE, Mysore.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


SRI JAYACHAMARAJENDRA COLLEGE OF
ENGINEERING(AUTONOMOUS)
(Affiliated to Visvesvaraya Technological University, Belgaum)
MYSORE

Page | 1
Acknowledgement

The successful completion of a project involves the efforts of many people. We have

been lucky to have received a lot of help and support from all quarters during the

course of this project, so with gratitude we take this opportunity to acknowledge all

those whose guidance and encouragement helped us emerge successful.

We convey my sincere thanks to our beloved principal

Dr. B.G.Sangameshwar, SJCE, MYSORE, for giving us support in our academic

progress. It’s our privilege and honor to express our sincere gratitude to our guide,

Smt.Trishiladevi C Nagavi, for her great support and encouragement. We express

our regards to our HOD, Dr. C.N.Ravikumar, Dept. of CS&E, SJCE, Mysore.

We are also grateful to all other teaching and nonteaching faculty of our department

and our friends who extended a lot of support to us.

Contents Page no

1. Introduction. 4-5

2. System Requirements. 5-7


Page | 2
i) Input

ii) Output

iii) Software requirements.

iv) Hardware requirements.

v) Functional requirements.

vi) Non functional requirements.

1. Design and Implementation 8-9

2. System testing 10-11

3. Results

4. Applications and Limitations 11-13

5. Conclusion. 13-14

6. Reference. 15-16

1. Introduction

The syntax analyzer is a module that reads the token stream delivered by the lexer

module and builds a memory data structure containing the syntactically analyzed and

built program.

Page | 3
For the syntax analyzer the program is a series of commands. A command is a series

of symbols. There is nothing like command blocks, or one command embedding

another command. Therefore the syntax definition is quite simple and yet still

powerful enough to define a BASIC like language.

The syntax analyzer should be configured using a structure the configuration

parameters and the “global variables” for the syntactical analyzer. This structure

contains the pointer to the array containing the syntax definition. Each element of the

array defines command syntax. Command syntax is the list of the symbols that

construct the command. When the syntactical analyzer tries to analyze a line it tries

the array elements until it finds one matching the line.

When checking a line against a syntax definition the syntactical analyzer takes the

lexical elements on the line and checks that they match the next symbol in the syntax

definition. A symbol can be as simple as a reserved word, like if else, endif. Such a

syntax element is matched by the specific keyword. On the other hand a symbol on

the syntax definition can be as complex as an expression, which is matched by a

whole expression.

The Syntax analyzer is the section of the compiler which verifies the structure of the

C program to be according to the grammar of the language.

Page | 4
Syntax Analysis or Hierarchical Analysis, in which characters or tokens are

grouped hierarchically into nested collections with collective meaning. Hierarchical

analysis also termed as Parsing, involves grouping the tokens of the source program

into grammatical phrases that are used by the compiler to synthesize output. Usually,

the grammatical phrases of the source program are represented by a Parse tree.

2. REQUIREMENTS:

2.1 INPUT:

Page | 5
The input to the program should be given through a file. The input is a written ‘C’

program whose Syntax has to be analyzed.

2.2 OUTPUT:

The output of the Syntax analyzer is to verify the overall syntax of the input C

program and say ‘VALID’ if it is, or display the line number of the syntax error

saying the program is ‘INVALID’ if the program does not satisfy the grammar.

2.3 Functional Requirements:

1. The program should accept a C file and validate its correctness of the syntax.

2. It should be effectively able to recognize the syntax of header files,

declarations, read-write statements, looping constructs like if, else, if else,

nested-if’s, for, while, do while, switch statements and comment lines.

3. It should accept all the valid identifiers, numbers and arithmetic operations.

4. It should display validity or invalidity of the syntax of the program.

5. It should display the line number of the invalid syntax in case.

2.4 Non Functional Requirements:

1. Easy to use User interface.

2. Efficiency in analyzing.

3. Versatility in handling all types of c source code.

4. Output suited to be provided for other tool as input.

2.5 Resources used for the project

Page | 6
1.5.1 Hardware resources

➢ Intel processor

➢ 256 MB RAM or Higher

➢ 40GB hard disk

2.5.2 Software resources

➢ Linux OS

➢ Editor

➢ Lex and YACC tool

➢ GNU C compiler (cc)

1. Design and Implementation.

The design of the syntax analyzer is done in 2 stages, viz

(i) Lex scanner

(ii) Yacc parser

Page | 7
Lex Specifications

Features of lex:

1. Identifies the keywords, variables and identifiers and returns the token as the

value as yylex( ).

2. Increments the value of the line counter for the reference of the line number in

case of error.

3. Effectively ignores the comment lines and white spaces without returning to

yacc.

Structure of the Lex specification:

%{ (definition section)

Headerfiles inclusion

Line counter declaration and initialization.

Page | 8
%}

(Identifiers)

Defining identifiers using Regular expressions

%%

(Rules section)

Pattern → Syntax of all possible statements.

Action → Increment line counter and return the action specific

token to yacc parser as the value of yylex ( )

%%

Yacc Specifications

Features of Yacc:

1. Verifies the complete structure of the C program using the tokens returned by the

lex specification.

2. Returns yyerror( ) to the user subroutine in case of violation of the specified

rules structure.

3. It uses the Look Ahead LR (LALR) parsing technique to create a

parser.

4. It stores the action and goto table in the form of compressed array.

Structure of Yacc:

%{ (Definition Section)

Headerfile

%}

Page | 9
% token: token codes obtained as the value of yylex( ) from Lex scanner.

%%

(Production Section)

Defining the structure of the C program

%%

(User Subroutine Section)

main ( )

Display messages for the successful parsing and

yyerror ( ) condition.

1. System Testing.

Page | 10
The final syntax analyzer is got through the a.out file

For Syntax analysis of any if else statement, the name of the file is specified in the

terminal along the ./a.out command.

TEST CASES
NO FEATURE EXPECTED ACTUAL REMARKS
RESULT RESULT
Page | 11
1 ACCEPT YES YES PASS
INPUT FROM
FILE
2 ACCEPT THE YES YES PASS
IF ELSE
STATEMENT
OF LEVEL 1
3 ACCEPT THE YES YES PASS
IF ELSE
STATEMENT
OF LEVEL n
4 ERROR YES YES PASS
HANDLER
FOR IF(A>B)
5 ERROR YES YES PASS
HANDLER
FOR IF(A>B);
6 ERROR YES YES PASS
HANDLER
FOR OPEN
PARANTHESIS
7 ERROR YES YES PASS
HANDLER
FOR CLOSE
PARANTHESIS

RESULTS

Page | 12
In the Terminal:

Page | 13
Possible error in if else statements

In terminal

Page | 14
APPLICATIONS:

➢ The designed syntax analyzer can be effectively used to verify the syntax of

the most of the possible C statements.

➢ To find the line number of the syntax error.

LIMITATIONS:

➢ The program returns line number of the first error occurred and proceeds

only after correcting the error.

➢ The comment lines if any should be given only at the beginning or ending of

the input C program.

➢ The parse tree construction for the scanf and printf statements are not

constructed and can be considered as the further scope of the project.

CONCLUSION:

Syntax analyzer is the major module used in any compiler. By repeated testing, we

have found out that SAC successfully analyses

any input C program as to whether it is syntactically correct or not, and provides an

easily understandable interface for the user to interact with SAC. The Syntax errors

are successfully found along with the line number. The expected results have been

Page | 15
obtained and hence the implementation of the Syntax analyzer for C programming

language has been found successful

REFERENCES:

1. Lex and Yacc - O’Reilly.

2. Software Engineering-Ian Somerville.

3. System Software - Lealand L Beck.

4. http://en.wikipedia.org/wiki/Syntax analysis.

Page | 16

Potrebbero piacerti anche