Compiler Design Lab Assignments: Syntax Checking with Bison and Flex, Exercises of Data Communication Systems and Computer Networks

A series of compiler design lab assignments focused on syntax checking using bison and flex. It includes programs to check the syntax of simple expressions, c++ variable declarations, and to recognize specific strings using formal grammars. Additionally, it covers recognizing nested if control statements and validating email writing. Each assignment provides the necessary code snippets and instructions, making it a practical resource for students learning compiler design principles and tools. The assignments are designed to enhance understanding of lexical analysis and parsing techniques.

Typology: Exercises

2021/2022

Uploaded on 09/26/2025

hundaol-dejene
hundaol-dejene 🇪🇹

1 document

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
College Of Informatics
Department of: computer Science
Compiler Design Lab Assignment
Name; Biniyam Kasehun
Id: ugr/68763/14
JUNE 1, 2025
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Compiler Design Lab Assignments: Syntax Checking with Bison and Flex and more Exercises Data Communication Systems and Computer Networks in PDF only on Docsity!

College Of Informatics

Department of: computer Science

Compiler Design Lab Assignment

Name; Biniyam Kasehun

Id: ugr/68763/

JUNE 1, 2025

1. Write bison program to check the syntax simple expression involving

operators +, -, *, and /

#include <stdio.h> #include <stdlib.h> int yylex(void); void yyerror(const char s); %} %token NUMBER %left '+' '-' %left '' '/' %left UMINUS %% input: | input line ; line: expr '\n' { printf("Valid expression.\n"); } ; expr: expr '+' expr { /* do nothing / } | expr '-' expr { / do nothing / } | expr '' expr { /* do nothing / } | expr '/' expr { / do nothing */ } | '-' expr %prec UMINUS | '(' expr ')' | NUMBER ; %%

2. Write bison program to check the syntax of valid c++ variable declaration

statement?

#include <stdio.h> #include <stdlib.h> int yylex(void); void yyerror(const char *s); %} %token INT FLOAT CHAR DOUBLE ID NUMBER CHARACTER %% stmt: decl_list ';' { printf("Valid C++ declaration.\n"); } ; decl_list: type var_list ; type: INT | FLOAT | CHAR | DOUBLE ; var_list: var | var_list ',' var ; var: ID | ID '=' init_val ;

init_val: NUMBER | CHARACTER ; %% void yyerror(const char s) { fprintf(stderr, "Syntax error: %s\n", s); } int main(void) { printf("Enter a C++ variable declaration:\n"); yyparse(); return 0; } Flex file %{ #include "decl.tab.h" %} %% "int" { return INT; } "float" { return FLOAT; } "char" { return CHAR; } "double" { return DOUBLE; } [0-9]+(.[0-9]+)? { return NUMBER; } '[^']' { return CHARACTER; } [a-zA-Z_][a-zA-Z0-9_] { return ID; } [ \t\r] { /* skip whitespace */ } ";" { return ';'; } "," { return ','; }

| A { printf("Matched: a\n"); } ; %% void yyerror(const char *s) { fprintf(stderr, "Syntax error: %s\n", s); } int main(void) { printf("Enter string using a and b (press Ctrl+D or Ctrl+Z to end):\n"); yyparse(); return 0; } Flex file %{ #include "match.tab.h" %}

%% output

a { return A; } b { return B; } [ \t\n] { /* skip whitespace */ }

. { return yytext[0]; } // Any other character %% int yywrap(void) { return 1; }

4. Write bison to recognize nested IF control statements and display the level

of nesting.

Bison:

#include <stdio.h> #include <stdlib.h> int yylex(); void yyerror(const char s); int nesting_level = 0; %} %expect 2 // Expected shift/reduce conflicts %token IF LPAREN RPAREN LBRACE RBRACE IDENTIFIER %start stmt %% stmt: IF LPAREN condition RPAREN block { nesting_level++; printf("Nesting Level: %d\n", nesting_level); } ; condition: IDENTIFIER { / Valid condition / } ; block: LBRACE stmt RBRACE { / Correct nesting level tracking / } | LBRACE RBRACE { / Empty block */ } | stmt { nesting_level++; } ;

5. Write bison program to validate email writing.

Bison:

#include <stdio.h> #include <stdlib.h> int yylex(); void yyerror(const char *s); %} %token USERNAME AT_SYMBOL DOMAIN DOT TLD NEWLINE INVALID %% email: USERNAME AT_SYMBOL DOMAIN DOT TLD NEWLINE { printf("Valid email address
n"); } | INVALID NEWLINE { printf("Invalid email address\n"); yyerrok; } ; %% int main() { printf("Enter an email address:\n"); return yyparse(); } void yyerror(const char *s) { printf("Invalid email address\n"); } Lex File %{

#include "email.tab.h" %} %% [a-zA-Z0-9_.]+ { return USERNAME; } "@" { return AT_SYMBOL; } [a-zA-Z0-9-]+ { return DOMAIN; } "\." { return DOT; } // Correctly escape "." (com|net|org|edu) { return TLD; } \n { return NEWLINE; } [ \t]+ { /* Ignore spaces */ }

. { return INVALID; } // Catch unexpected characters

%% Output:

int yywrap() { return 1; } int yylex();