Skip to content

JinhyukKo/compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

isExpression Parser

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.

1*xmlS7NS4jrEHfe8W8CZWog

Features

  • 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

Dependencies

  • Flex - for lexical analysis
  • Bison - for syntax parsing
  • GCC - for compilation

Getting Started

Follow these instructions to set up, compile, and run the isExpression parser on your system.

1. Clone the Repository

git clone [email protected]:JinhyukKo/compiler.git
cd isExpression

2. Compile the Code

Step 1: Generate Bison Parser Files

bison -d syntax.y

This will create syntax.tab.c and syntax.tab.h.

Step 2: Generate Flex Lexer Files

flex tokenizer.l

This will create lex.yy.c.

Step 3: Compile with GCC

gcc -o isExpression syntax.tab.c

3. Run the Program

./isExpression

How to Use

  1. Input an expression following the supported format (e.g., 1 + 2;).
  2. Press Enter to evaluate. The program will print each operation it evaluates, followed by a success message if no errors are detected.

Example

Enter an expression:
1 + 2 * (3 - 4) / 5;
Expression evaluated.

Supported Expressions

  • Arithmetic Operations: +, -, *, /
  • Integers (e.g., 42)
  • Identifiers (e.g., variable names like x, result)
  • Parentheses for grouping (e.g., (2 + 3) * 4)

Files Overview

  • syntax.y: Bison grammar file defining the parser structure.
  • tokenizer.l: Flex file for lexical analysis.
  • syntax.tab.h and syntax.tab.c: Generated by Bison; contains token definitions and parsing functions.
  • lex.yy.c: Generated by Flex; contains the lexer functions.

Error Handling

If the input expression contains any syntax errors, the program will print Error: syntax error, helping you quickly spot issues.

License

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!

CFG

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 ;

Subprojects

intDetector

$ flex intDetector.l
$ gcc -o intDetector lex.yy.c
$ ./intDetector

determine if the input strings are integers or not

Screenshot 2024-10-19 at 6 45 24 PM

Tokenizer

$ flex expression.l
$ gcc -o expression lex.yy.c
$ ./expression < data.txt

$$f(L)=t$$

L: langauge t : tokens Get string as an input and find tokens in expressions

Screenshot 2024-10-19 at 6 50 24 PM

Tip

in LEX, you cannot determine Balanced Parentheses as you do in CFG $$L={(^n)^n}$$

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages