The isExpression program is a simple parser and evaluator for arithmetic expressions written in C, leveraging Flex and Bison for lexical analysis and parsing. This tool supports basic arithmetic operations, parentheses, and variable identifiers, making it a great learning resource for compiler basics.
- Supports basic arithmetic operations: Addition (
+), Subtraction (-), Multiplication (*), and Division (/) - Recognizes numeric constants and identifiers
- Handles expressions with parentheses for operation precedence
- Detects syntax errors with clear feedback
- Flex - for lexical analysis
- Bison - for syntax parsing
- GCC - for compilation
Follow these instructions to set up, compile, and run the isExpression parser on your system.
git clone [email protected]:JinhyukKo/compiler.git
cd isExpressionbison -d syntax.yThis will create syntax.tab.c and syntax.tab.h.
flex tokenizer.lThis will create lex.yy.c.
gcc -o isExpression syntax.tab.c./isExpression- Input an expression following the supported format (e.g.,
1 + 2;). - Press Enter to evaluate. The program will print each operation it evaluates, followed by a success message if no errors are detected.
Enter an expression:
1 + 2 * (3 - 4) / 5;
Expression evaluated.
- Arithmetic Operations:
+,-,*,/ - Integers (e.g.,
42) - Identifiers (e.g., variable names like
x,result) - Parentheses for grouping (e.g.,
(2 + 3) * 4)
syntax.y: Bison grammar file defining the parser structure.tokenizer.l: Flex file for lexical analysis.syntax.tab.handsyntax.tab.c: Generated by Bison; contains token definitions and parsing functions.lex.yy.c: Generated by Flex; contains the lexer functions.
If the input expression contains any syntax errors, the program will print Error: syntax error, helping you quickly spot issues.
This project is open-source and available under the MIT License.
This README covers everything from setting up and compiling the parser to understanding its functionality and usage. Let me know if you’d like more specific sections or customizations!
this is the **CFG (Context Free Grammar ) ** of the file
S : E ';'
E : E '+' T | E '-' T | T ;
T : T '*' F | T '/' F | F ;
F : '(' E ')' | TIDEN | TNUM ;$ flex intDetector.l
$ gcc -o intDetector lex.yy.c
$ ./intDetectordetermine if the input strings are integers or not
$ flex expression.l
$ gcc -o expression lex.yy.c
$ ./expression < data.txtL: langauge t : tokens Get string as an input and find tokens in expressions
Tip
in LEX, you cannot determine Balanced Parentheses as you do in CFG


