This project implements a lexical analyzer (lexer) for simple arithmetic expressions, including support for basic control structures like if-else
and while
loops. The lexer reads a source file containing arithmetic expressions and control structures, tokenizes the input, and provides a framework for parsing and processing the tokens.
- Tokenizes arithmetic expressions with operators:
+
,-
,*
,/
- Supports integer literals and identifiers
- Includes basic control structures:
if-else
andwhile
loops - Reads input from a text file (
ex1.txt
)
<program> -> <start_op> <else_ifadesi> <finish_op>
| <start_op> <var> <assign_op> <expr> <finish_op>
| <start_op> <while_ifadesi> <finish_op>
| <start_op> <ifelse> <finish_op>
<else_ifadesi> -> <eger> <var> <karsilastirma_op> <factor> <ise> <var> <assign_op> <factor> <degilse> <var> <assign_op> <factor>
<start_op> -> <
<finish_op> -> >
<var> -> <type> <id>
<type> -> int
<id> -> <letter> <id2>
<id2> -> <letter> <id2> | <digit> <id2> | ?
<expr> -> <term> - <expr> | <term> + <expr> | <term>
<term> -> <factor> / <term> | <factor> * <term> | <factor>
<factor> -> <digit> <factor> | <digit>
<digit> -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<letter> -> a | b | c | ç | d | e | f | g | ? | h | ? | i | j | k | l | m | n | o | ö | p | r | s | ? | t | u | ü | v | y | z
<assign_op> -> =
<ise> -> ise
<degilse> -> degilse
<eger> -> eger
<karsilastirma_op> -> < | > | <= | >= | == | !=
<while_ifadesi> -> <while_op> <var> <karsilastirma_op> <factor> <do_op> <program>
<while_op> -> while
<do_op> -> do
LETTER
(0)DIGIT
(1)UNKNOWN
(99)INT_LIT
(10)IDENT
(11)VAR
(12)ASSIGN_OP
(20)ADD_OP
(21)SUB_OP
(22)MULT_OP
(23)DIV_OP
(24)LEFT_PAREN
(25)RIGHT_PAREN
(26)START_OP
(30)FINISH_OP
(31)EGER
(40)KARSILASTIRMA_OP
(41)DEGILSE
(45)ATAMA_DURUMU
(46)KARSILASTIRMA_DURUMU
(48)EGER_IFADESI
(50)ISE
(51)WHILE
(52)DO
(53)
front.c
: The main source code file containing the lexer and parser implementation.ex1.txt
: Example input file containing test expressions and control structures.
- Ensure you have a C compiler installed (e.g., GCC).
- Compile the source code:
gcc front.c -o lexer
- Create or modify the
ex1.txt
file to include the arithmetic expressions and control structures you want to analyze. - Run the compiled lexer:
./lexer
- Global Variables: The lexer uses several global variables for tracking the current state, including
charClass
,lexeme
,nextChar
,lexLen
,token
,nextToken
, andin_fp
. - Character Classes: Defined for letters, digits, and unknown characters.
- Token Codes: Constants representing different token types.
- Function Declarations: Includes standard functions for lexical analysis (
addChar
,getChar
,getNonBlank
,lex
,lookup
) and custom functions for parsing and error handling (program
,start_op
,finish_op
,var
,type
,id
,id2
,expr
,term
,factor
,digit
,letter
,assign_op
,eger_ifadesi
,karsilastirma_op
,degilse
,ise
,while_ifadesi
,while_op
,do_op
,err
).
ex1.txt
:
{
sayi x = 10
eger x > 5 ise
sayi y = 20
degilse
sayi z = 30
}
The err
function prints an error message and exits the program if an unexpected token or syntax error is encountered during parsing.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/AmazingFeature
). - Commit your changes (
git commit -m 'Add some AmazingFeature'
). - Push to the branch (
git push origin feature/AmazingFeature
). - Open a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
By following this README, you should be able to understand, build, and run the lexical analyzer for simple arithmetic expressions. Feel free to contribute or modify the code as needed for your use case.