CSE-3104 Compiler Lab LAB-01 PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides information on a compiler lab for a computer science course, including the schedule, marking scheme, and an introduction to Lex, YACC, and Flex. The lab appears to cover the installation and usage of these tools in a compiler-related context.
Full Transcript
CSE-3104 Compiler Lab LAB-01 Every Thursday 8:30 AM to 11:45 AM Class Time Section A/B in alternate week (to be announced later in class) Attendance → 10% Lab evaluation/Class performance/Lab assignments → 15% MARKS...
CSE-3104 Compiler Lab LAB-01 Every Thursday 8:30 AM to 11:45 AM Class Time Section A/B in alternate week (to be announced later in class) Attendance → 10% Lab evaluation/Class performance/Lab assignments → 15% MARKS Lab Report → 15% DISTRIBUTION Lab Quiz → 20% Viva → 10% Lab Final → 30% Experiment number Title Introduction Lab Report Objective Format Task/Code Input/Output Discussion Lab task questions (will be given in class) Course Schedule Week Lecture Topics Lab-1, 2 1 Introduction to Lex, YACC and Flex/Bison Installation 3 Lab-3, 4 Basic Lex/YACC program Lab-5, 6 5 Tokenizer Using C/C++ Lab-7, 8 7 Tokenizer Using Flex Lab-9, 10 9 Arithmetic Calculator Using Flex/Bison Lab-11, 12 Recognize a valid arithmetic expression that uses operator +, -,*and / 11 using Lex/YACC, Lab Quiz Lab-13, 14 13 Lab Final Obtaining Help with the Labs Good methods for obtaining help include: Asking me during office hours Asking another student, but...... Your lab must be your own. That is, each student must submit a unique lab task. Naturally, simply changing comments, variable names, etc. does not produce a unique lab. Google Classroom Code wd66vkb Students are requested to join google classroom within this week. LECTURE 1 Introduction to Lex, YACC and Flex/Bison Installation Lex, Flex & YACC ❑Lex: Lex in compiler design is a program used to generate lexical analyzers, also called tokenizers. ❑These tokenizers identify the lexical pattern in the input program and convert the input text into the sequence of tokens. Lex, Flex & YACC ❑Flex: Flex stands for fast lexical analyzer generator, it is a computer application that is used to generate lexical analyzers for the programs written in lex language. Lex, Flex & YACC ❑YACC: YACC stands for yet another compiler, it is a parser generator that is used with lex to generate parsers of lex files. Installation ❑Step 1: Download and install Flex in your PC ❑.exe file download link: https://sourceforge.net/projects/gnuwin32/files /flex/2.5.4a-1/flex-2.5.4a-1.exe/download Installation ❑Step 2: Install Codeblocks (codeblocks-20.03mingw- setup.exe file includes additionally the GCC/G++/GFortran compiler) Skip this step if you already have codeblocks installed Setup ❑Step 3: add MinGW's bin directory to the system's environment PATH, so that you can run MinGW commands (like gcc) directly from any command prompt or terminal window. Setup ❑Step 4: Add the flex bin directory to your system's environment PATH How it works? Let’s run our first Lex code Problem 1: %{ #include Declaration/Definition #include Section int i = 0; %} %% ([a-zA-Z0-9])* {i++;} Rules Section "\n" {printf("%d\n", i); i = 0;} %% int yywrap(void){} int main() { // The function that starts the analysis User Code Section yylex(); return 0; } Let’s run our first Lex code Problem 1: %{ #include Declaration/Definition #include Section int i = 0; %} #include and #include: These are standard C library headers for input/output functions and string manipulation functions, respectively. int i = 0;: A global variable i is declared and initialized to 0. This variable will be used to count the number of words. Let’s run our first Lex code Problem 1: %% ([a-zA-Z0-9])* {i++;} Rules Section "\n" {printf("%d\n", i); i = 0;} %% ([a-zA-Z0-9])* {i++;}: ([a-zA-Z0-9])*: This regular expression matches any sequence of alphanumeric characters (letters and digits). {i++;}: Each time a word is matched, the counter i is incremented by 1. The * indicates zero or more occurrences, meaning even single characters are considered words here. "\n" {printf("%d\n", i); i = 0;}: "\n": This matches the newline character, which indicates the end of a line. {printf("%d\n", i); i = 0;}: When a newline is encountered, it prints the current count of words (i) and then resets i to 0 for the next line of input. Let’s run our first Lex code Problem 1: int yywrap(void){} int main() { User Code Section // The function that starts the analysis yylex(); return 0; } int yywrap(void){}: This function is required by Lex and is called when the end of the input is reached. int main(): The main function is the entry point of the C program. yylex();: This function call starts the Lex-generated scanner, which reads input, applies the rules defined in the rules section, and processes the input accordingly. return 0;: The program returns 0 indicating successful execution. Let’s run our first Lex code Problem 1: Run from Command Prompt Thank You!!!!