0% found this document useful (0 votes)
5 views

compiler record work

Compiler

Uploaded by

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

compiler record work

Compiler

Uploaded by

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

Ex. No:1 Develop a lexical analyzer to recognize a few patterns in C.

(Ex.identifiers, constants, comments, operators etc.).

AIM:
To Write a LEX program to recognize few patterns in C.

ALGORITHM:

Step 1: Take the C code as input.


Step 2: Initialize an empty list to store tokens.
Read the input code character by character.
Step 3: Lexical Analysis:
Identifiers:
Start reading characters. If the first character is a letter (a to z or A to Z) or an
underscore (_), continue reading characters until a non-letter, non-digit, or non-
underscore character is encountered. Add the identified word as an identifier token
to the list.
Constants (Integers and Floating-Point Numbers):
If the current character is a digit, start reading characters until a non-digit or a dot (.)
is encountered. If a dot is encountered, continue reading digits as it might be a
floating-point number. Add the identified number as a constant token to the list.
Operators and Special Characters:
Check for operators and special characters such as +, -, *, /, =, ==, !=, <, >, etc.
Identify these characters individually and add them as operator tokens to the list.
Comments:
Check for comment patterns such as // for single-line comments and /* */ for multi-
line comments. Ignore the characters inside comments as they are not part of the
tokens.
Whitespace and Newlines:
Ignore whitespace characters, tabs, and newline characters as they are typically not
part of the tokens. They are used for code formatting.
Step 4: Output the list of tokens identified during the lexical analysis. Each token
should be labelled with its type (identifier, constant, operator, etc.) and the actual
value (if applicable).

RESULT:
Thus the program for developing a lexical analyzer to recognize a few
patterns in C has been executed successfully.
Ex. No: 2 Implement a Lexical Analyzer using Lex Tool

AIM
To write a program for implementing a Lexical analyser using LEX tool.

ALGORITHM
Step 1: Declare and Initialize the required variable.
Step 2: If the statement starts with #.* print it as preprocessor directive.
Step 3: Check for the given list of keywords and print them as keyword if it is
encountered.
Step 4: If the given string is ‘/*’ or ‘*/’ print it as comment line.
Step 5: For a function, print the beginning and ending of the function block.
Step 6: Similarly print the corresponding statements for numbers, identifiers and
assignment operators.
Step 7: In the main function get the input file as argument and open the file in read
mode.
Step 8: Then read the file and print the corresponding lex statement given above.

Result:

Thus the program for implementing a Lexical analyser using LEX tool was
executed

successfully.
Ex.No:3a Program to recognize a valid arithmetic expression that uses
operator + , -, * and /.

AIM
To write a program for recognizing a valid arithmetic expression that uses the
operator +,-* and / using semantic rules of the YACC tool and LEX.

ALGORITHM
Step 1: A Yacc source program has three parts as follows: Declarations %%
translation rules
%% supporting C routines
Step 2: Declarations Section: This section contains entries that:
Include standard I/O header file.
Define global variables.
Define the list rule as the place to start processing.
Define the tokens used by the parser.
Step 3: Rules Section: The rules section defines the rules that parse the input
stream. Each rule of a grammar production and the associated semantic action.
Step 4: Programs Section: The programs section contains the following
subroutines. Because these subroutines are included in this file, it is not
necessary to use the yacc library when processing this file.
Main- The required main program that calls the yyparse subroutine to start the
program.
yyerror(s) -This error-handling subroutine only prints a syntax error message.
yywrap -The wrap-up subroutine that returns a value of 1 when the end of input
occurs. The calc.lex file contains include statements for standard input and
output, as programmer file information if we use the -d flag with the yacc
command. The y.tab.h file contains definitions for the tokens that the parser
program uses.
Step 5: calc.lex contains the rules to generate these tokens from the input
stream.

RESULT:
Thus the program for recognizing valid arithmetic expression is executed
successfully.
Ex.No:3b Program to recognize a valid variable which starts with a letter
followed by any number of letters or digits.

AIM
To write a program for recognizing a valid variable which starts with a letter followed
by any number of letters /digits

ALGORITHM
Step 1: A Yacc source program has three parts as follows: Declarations %%
translation rules
%% supporting C routines
Step 2: Declarations Section: This section contains entries that:
Include standard I/O header file.
Define global variables.
Define the list rule as the place to start processing.
Define the tokens used by the parser.
Step 3: Rules Section: The rules section defines the rules that parse the input
stream. Each rule of a grammar production and the associated semantic action.
Step 4: Programs Section: The programs section contains the following subroutines.
Because these subroutines are included in this file, it is not necessary to use the
yacc library when processing this file.
Main- The required main program that calls the yyparse subroutine to start the
program.
yyerror(s) -This error-handling subroutine only prints a syntax error message.
yywrap -The wrap-up subroutine that returns a value of 1 when the end of input
occurs. The calc.lex file contains include statements for standard input and output,
as programmer file information if we use the -d flag with the yacc command. The
y.tab.h file contains definitions for the tokens that the parser program uses.
Step 5:calc.lex contains the rules to generate these tokens from the input stream.

RESULT:

Thus the program for to recognize a valid variable is executed


successfully.
Ex.No:3c Program to recognize a valid control structures syntax of c
language(For loop, While loop, if-else, if-else-if, switch case,
etc.,
AIM
To write a program to recognize a valid control structures syntax of c language (For
loop,
While loop, if-else, if-else-if, switch case, etc.
ALGORITHM
Step 1: Take the C code as input.
Step 2: Tokenize the input code. Split the code into individual tokens (keywords,
identifiers, operators, etc.) for easier processing.
Step 3: Syntax Checking:
For Loop:
Check for tokens that match the pattern: for (initialization; condition; increment) {/*
code */}
Ensure proper semicolons separating initialization, condition, and increment parts.
Recursively check the code inside the loop using the same steps.
While Loop:
Check for tokens that match the pattern: while (condition) {/* code */}
Ensure proper parentheses and braces.
Recursively check the code inside the loop.
If-Else Statements:
Check for tokens that match the pattern: if (condition) {/* code */} else {/* code */}
Ensure proper parentheses and braces for both if and else parts.
Recursively check the code inside both if and else blocks.
Else-If Ladder:
Check for tokens that match the pattern:
if (condition) { /* code */ } else if (condition) { /* code */ } ... else { /* code */ }
Ensure proper parentheses and braces for each if and else block.
Recursively check the code inside each block.
Switch-Case Statements:
Check for tokens that match the pattern:
switch (variable) { case constant1: /* code */ break; case constant2: /* code */
break; ... default: /* code */ break; }
Ensure proper parentheses, colons after cases, and break statements.
Recursively check the code inside each case and default block.
Step 4: If the code passes all syntax checks, output a message indicating that the
control structures are valid. Otherwise, indicate the specific error encountered during
the parsing process.

RESULT:

Thus the program to recognize a valid control structures was executed


successfully.
Ex. No:3d Implement an Arithmetic Calculator using LEX and YACC

AIM
To write a program for implementing a calculator for computing the given expression
using semantic rules of the YACC tool and LEX.

ALGORITHM
1.A Yacc source program has three parts as follows:
Declarations
%% translation rules %%
supporting C routines
2.Declarations Section: This section contains entries that:
Include standard I/O header file.
Define global variables.
Define the list rule as the place to start processing.
Define the tokens used by the parser.
Define the operators and their precedence.
3.Rules Section: The rules section defines the rules that parse the input stream.
Each rule of a grammar production and the associated semantic action.
4.Programs Section: The programs section contains the following subroutines.
Because these subroutines are included in this file, it is not necessary to use the
yacc library when processing this file.
Main- The required main program that calls the yyparse subroutine to start the
program.
yyerror(s) -This error-handling subroutine only prints a syntax error message.
yywrap -The wrap-up subroutine that returns a value of 1 when the end of input
occurs. The lex file contains include statements for standard input and output, as
programmar file information if we use the -d flag with the yacc command. The y.tab.h
file contains definitions for the tokens that the parser program uses.
5. The lex file contains the rules to generate these tokens from the input stream.

Result:

Thus the program for implementing a calculator for computing the given expression
using semantic rules of the YACC tool and LEX was executed successfully.
Ex. No: 4 Generate three address code for a simple program using LEX
and YACC

AIM:
To write program for Generate three address code for a simple program using LEX
and YACC.

ALGORITHM:
Step 1: Lexer (Lex):
Define lexical rules using Lex to tokenize the input code. Specify regular expressions
for identifiers, constants, operators, and keywords. Lex generates tokens for
recognized patterns.
Step 2: Parser (Yacc):
Define grammar rules using Yacc for expressions, statements, assignments, and
control structures. Attach actions to productions for generating three-address code.
Use semantic actions to handle grammar rules.
Step 3: Symbol Table:
Create a symbol table to store identifiers, their types, and memory locations. Update
the symbol table during the parsing process. Assign memory addresses to variables
during declarations.
Step 4: Intermediate Code Generation:
Integrate intermediate code generation logic within Yacc actions. When parsing
expressions or statements, generate corresponding three-address code instructions.
Store generated code in an intermediate code representation.
Step 5: After parsing the input code, the intermediate code will be generated based
on the specified rules. Store or process the generated three-address code for further
optimization or translation to machine code if necessary.

RESULT:

Thus the program for Generating three address code for a simple program
using LEX and

YACC was executed successfully.


Ex.No:5 Implement type checking using Lex and Yacc.

AIM:
To write a program for implement type checking using Lex and Yacc.

ALGORITHM:
Step 1: Define Grammar Rules:
Specify the grammar rules for the programming language, including expressions,
statements, declarations, and data types. Incorporate type information where
applicable.
Step 2: Lexer (Lex):
Implement lexical analysis using Lex to tokenize the input code. Define regular
expressions for identifiers, keywords, operators, constants, and other language
constructs. Lex generates tokens for recognized patterns.
Step 3: Symbol Table:
Create a symbol table data structure to store identifiers, their declared types, and
other
relevant information during the parsing process. Initialize the symbol table.
Step 4: Parser (Yacc):
Define Yacc grammar rules based on the language's syntax. Include actions within
Yacc rules to handle type checking logic.
During parsing, populate the symbol table with identifier names and their declared
types.
Handle type conversions and promotions within Yacc actions.
Step 5: Type Checking Logic:
Implement type checking logic within Yacc actions. Perform type comparisons and
validations according to the language's rules. For example:
 Check compatibility of operand types in expressions.
 Verify function return types match declared types.
 Ensure compatibility in assignments and comparisons.
 Handle type coercion and promotion.
Step 6: Error Handling:
Implement error handling mechanisms within Yacc actions for type mismatch errors.
Generate meaningful error messages when type mismatches are detected,
indicating the source of the error.

Result:

Thus the program for implement type checking using Lex and Yacc was
written and executed successfully.
Ex.No:6 Implement simple code optimization techniques
(Constant

folding, Strength reduction and Algebraic transformation)

AIM
To write a program for implementation of Code Optimization Technique.

ALGORITHM
1. Generate the program for factorial program using for and do-while loop to specify
optimization technique.
2. In for loop variable initialization is activated first and the condition is checked next.
If the condition is true the corresponding statements are executed and specified
increment / decrement operation is performed.
3. The for loop operation is activated till the condition failure.
4. In do-while loop the variable is initialized and the statements are executed then
the condition checking and increment / decrement operation is performed.
5. When comparing both for and do-while loop for optimization dowhile is best
because first the statement execution is done then only the condition is checked. So,
during the statement execution itself we can find the inconvenience of the result and
no need to wait for the specified condition result.
6. Finally when considering Code Optimization in loop do-while best with is respect
to performance.

Result:

Thus the C program for implementation of Code Optimization Technique


such as Constant folding, Strength reduction and Algebraic Transformation
was executed successfully.
Ex.No: 7 Implement back-end of the compiler for which the three
address code is given as input and the 8086 assembly language
code is produced as output.

AIM

To write a C program to implement the Back end of the compiler.

ALGORITHM

1. Start the Program

2. Get the three variables from statements and stored in the text file k.txt.

3. Compile the program and give the path of the source file.

4. Execute the program.

5. Target code for the given statement was produced.

6. Stop the program.

Result:

Thus the C program to implement the Back end of the compiler- for which
the three address code is given as input and the 8086 assembly language
code is produced as output was successfully executed.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy