compiler record work
compiler record work
AIM:
To Write a LEX program to recognize few patterns in C.
ALGORITHM:
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:
RESULT:
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
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
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:
AIM
ALGORITHM
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.
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.