






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







#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 ; %%
#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" %}
a { return A; } b { return B; } [ \t\n] { /* skip whitespace */ }
. { return yytext[0]; } // Any other character %% int yywrap(void) { return 1; }
#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++; } ;
#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
int yywrap() { return 1; } int yylex();