Coder Social home page Coder Social logo

nikosmav / compilers-1 Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 93 KB

A compiler construction project for the "Κ31 Compilers" course at DiT, NKUA. This repository hosts a Java application designed to parse and evaluate expressions based on a defined grammar for a subset of a programming language. It includes a lexer and parser to handle expressions, perform calculations, and construct a syntax tree.

Java 88.85% Makefile 0.72% Lex 10.44%
compilers-design java ll1-grammar ll1-parser

compilers-1's Introduction

Compilers Assignment - Expression Handling and Program Structure

Part 1: Grammar for Expression Handling

The grammar used for handling the expressions of the language in part 1 is as follows:

expr ::= term expr2
expr2 ::= + term expr2 | - term expr2 | ε
term ::= factor term2
term2 ::= ** factor term2 | ε
factor ::= num | (expr)
num ::= 0 | digit_without_0 num2
num2 ::= digit num2 | ε
digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
digit_without_0 ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Program Execution and Usage

The provided folder includes CalcEvaluator, Main, and ParseError files. To compile and execute the program, use the commands javac Main.java and java Main. The program awaits user input and the input can be terminated with either \n\n or EOF.

Part 2: Grammar for Language Expression Handling

The grammar used for handling the expressions of the language in part 2 is outlined as follows:

program ::= declarations main
declarations ::= declaration declarations | ε
declaration ::= DECLARATION_FUNCTION_START expression }
main ::= expression main | ε
expression ::= if_statement | concatenation | function_call | IDENTIFIER | STRING_LITERAL
if_statement ::= IF condition_function ) expression ELSE expression
condition_function ::= expression condition_function_tail
condition_function_tail ::= SUFFIX expression | PREFIX expression
concatenation ::= expression PLUS expression
function_call ::= FUNCTION_START args )
args ::= expression args_tail | ε
args_tail ::= COMMA expression args_tail | ε

Two terminal symbols - tokens DECLARATION_FUNCTION_START and FUNCTION_START are used for distinguishing simple function calls from function declarations. The primary distinction is the presence of a brace {. Lexical ambiguity issues are resolved by creating two different symbols in the Lexer using regular expressions.

The precedence of these tokens is defined as follows:

precedence(if) < precedence(prefix/suffix) < precedence(concat) < precedence(function_call).

Program Execution and Usage

The provided folder contains a makefile identical to the one provided in the laboratory material for compilation and execution of all files. The program reads input from the provided input.txt file. The output, a generated Java file, is stored in the output/ directory where the user can navigate and compile and run the program with the javac Main.java and java Main commands respectively.

License

This project is for educational use only and is part of the coursework for Κ31 Compilers at DiT, NKUA.

compilers-1's People

Contributors

nikosmav avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.