Project for CSE 334 - Programming Languages
Project Members: Gizem Akturk and Mehmet Karagoz
This project is an example implementation of a Yacc (Yet Another Compiler Compiler) parser using the yacc tool in C. The parser reads input files written in a specific grammar and performs parsing and actions based on the grammar rules.
The purpose of this project is to demonstrate how to build a basic programming language parser using Yacc. It showcases the use of lexical analysis (implemented with Lex) and syntax analysis (implemented with Yacc) to process input files according to a defined grammar.
The implemented grammar supports variable assignments, arithmetic expressions, print statements, if-else statements, while loops, error-handling and function calls. It provides a foundation for building more complex programming languages or interpreters.
<prog> ::= <statement_list>
<statement_list> ::= <statement>
| <statement_list> <statement>
<statement> ::= <assign_statement>
| <print_statement>
| <if_statement>
| <while_statement>
| <function_call>
<assign_statement> ::= VARIABLE ASSIGN <expression>
<print_statement> ::= PRINT_EXP <expression>
| PRINT_EXP STRING
| PRINT_EXP <boolean_expression>
<term> ::= NUMBER
| VARIABLE
<expression> ::= <term>
| <expression> '+' <expression>
| <expression> '-' <expression>
| <expression> '*' <expression>
| <expression> '/' <expression>
<boolean_expression> ::= BOOLEAN_VAL
| <expression> EQUALS <expression>
| NOT <boolean_expression>
<if_statement> ::= IF <boolean_expression> <statement> %prec IF
| IF <boolean_expression> <statement> ELSE <statement> %prec IF
<while_statement> ::= WHILE <boolean_expression> <statement> %prec WHILE
<function_call> ::= FUNCTION LPAREN NUMBER RPAREN <expression>
<prog>
: Represents the starting non-terminal symbol, which denotes the program.<statement_list>
: Represents a list of statements in the program.<statement>
: Represents a single statement in the program, which can be an assignment statement, print statement, if statement, while statement, or function call.<assign_statement>
: Represents an assignment statement, where a variable is assigned a value.<print_statement>
: Represents a print statement, which outputs the result of an expression or a string to the console.<term>
: Represents a term in an arithmetic expression, which can be a number or a variable.<expression>
: Represents an arithmetic expression composed of terms and operators.<boolean_expression>
: Represents a boolean expression, which can be a boolean value, a comparison between expressions, or a negation of a boolean expression.<if_statement>
: Represents an if statement, which executes a statement if a given boolean expression evaluates to true. It can also include an optional else statement for alternative execution.<while_statement>
: Represents a while statement, which repeatedly executes a statement as long as a given boolean expression evaluates to true.<function_call>
: Represents a function call, which invokes a function with a specified argument and an expression to determine a local variable calledbody
.
These production rules define the structure and behavior of the language defined by the grammar. Understanding these parts will help in comprehending the functionality and flow of the program implemented using this yacc file.
The following table lists the types of tokens used in the grammar and their corresponding values.
<variable>
: Represents a variable token.<number>
: Represents a number token.<string>
: Represents a string token.<func>
: Represents a function token.
Note: This project requires the
yacc
andlex
tools to be installed on the system. The project was developed and tested on Ubuntu LTS.
The project can be built and run using the following commands:
make
./pikachu example.mg
or
make
./pikachu < example.mg
The make
command will build the project using the Makefile
and generate the executable file pikachu
. The pikachu
executable can then be run with an input file as an argument. The input file should contain a program written in the grammar defined in the yacc file.
The following is an example input file that can be used to test the program. It contains a program written in the grammar defined in the yacc file.
Input:
x = 5
y = 10
z = x + y
Pikachu: z
Output:
Set x to 5
Set y to 10
Set z to 15
15
Input:
a = 2
Pika a == 2
a = 3
Ash
a = 4
Output:
Set a to 2
Set a to 3
Indside If
Input:
a = 3
WhilePika not a == 4
a = a + 1
Output:
Set a to 3
Set a to 4
Inside While 1
Input:
a = 5
Bulbasour (3) a
Output:
Set a to 5
Function called argument with 3. The result of this function: 15
Input:
@ Error handling example in comment
Pikachu: +-*0
Output:
Error: syntax error at line 3
This project demonstrates the implementation of a programming language using yacc. It defines a grammar for the language and uses yacc to generate a parser for the language. The parser is then used to parse a program written in the language and execute it. The program also demonstrates the use of error handling and comments in the language.
Note: If you want to clean codes, you need to run
make clean
.