yacc small case

 

YACC convert small case

First, make sure you have Flex installed:
For installing on Ubuntu- sudo apt-get install flex bison

How to Compile and Run:

Clean and rebuild:
Copy
yacc -d parser.y lex lexer.l gcc lex.yy.c y.tab.c -o case_converter -ll ./case_converter
Copy
---Save as lexer.l--- %{ #include "y.tab.h" %} %% [a-zA-Z] { yylval = yytext[0]; return LETTER; } . { yylval = yytext[0]; return OTHER; } /* Catch-all for other chars */ \n { return '\n'; } %% int yywrap() { return 1; }
Copy
---Save as parser.y--- %{ #include <stdio.h> #include <ctype.h> int yylex(); void yyerror(char *); char converted[100]; int pos = 0; %} %token LETTER OTHER %% input: /* empty */ | input line ; line: conversion '\n' { printf("Converted: %s\n", converted); pos = 0; } | '\n' { pos = 0; } ; conversion: /* empty */ | conversion LETTER { converted[pos++] = islower($2) ? toupper($2) : tolower($2); converted[pos] = '\0'; } | conversion OTHER { converted[pos++] = $2; converted[pos] = '\0'; } ; %% void yyerror(char *s) { fprintf(stderr, "Error: %s\n", s); } int main() { printf("Enter text to convert cases (Ctrl+D to exit):\n"); yyparse(); return 0; }
Copy
Pune Converted: pUNE Hello World! Converted: hELLO wORLD!

Comments

Popular posts from this blog

literal

yacc evaluate

built in functions