Coder Social home page Coder Social logo

alba-nr / compilers_summerwork Goto Github PK

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

:computer: Implementation of a lexical analyser and an SLR parser in Java for my IA to IB compilers summer work task. (more details in README).

Java 100.00%
lexer slr-parser grammar parse-tree

compilers_summerwork's Introduction

๐Ÿ’ป Compilers IA to IB summer work: Lexical Analyser and SLR parser in Java.

This is my implementation of a lexer/lexical analyser and an SLR parser in Java for my IA to IB compilers summer work task. (Note: "Part IA" is the 1st year of the Computer Science Tripos at Cambridge. "Part IB" corresponds to the second year.)

๐Ÿ“Œ Note: This repository is set to public to showcase this project; however, this doesn't mean the code can be freely copied and used, please see the Copyright Notice below.


๐Ÿ“‹ Task Description

"You are to write a lexer and a parser for a new calculator application. The calculator is to read in ASCII strings containing arithmetic expressions (e.g. โ€œ2.3+4โ€) and you should emit a parse tree for the calculation. Accepted input numbers are to be signed floating point numbers. The operators, in order of increasing precedence are:*

Operator Description
+ A diadic infix operator, left associative
- A diadic infix operator, left associative
* A diadic infix operator, right associative
cos A monadic prefix operator
! A monadic postfix operator
  1. Define production rules for a grammar to accept signed floating point numbers.
  2. An expression is a valid use of one of the operators above. Give production rules to show how each of the above operators can be used, noting the associativity and precedence.
  3. List the terminal and non-terminal symbols of the language, and define an entry point for the grammar of calculations: a statement is a number or a valid application of one of the operators.
  4. Write a program to lex an input ASCII string into tokens accepted by this language.
  5. Write a parser based on an LR(0) technique to convert the token stream into a parse tree.

๐Ÿ’ญ My Solution

I organised my compiler front-end (the lexical analyser & the parser) into 2 Java packages:

src
 |__ lexer
 |__ parser
 ...

The LexicalAnalyser.java class in the lexer package implements the lexer itself. Its static method scan scanse the given .txt file (its 1st line) and produces a corresponding list of tokens. Its implementation is specific to the expression language being considered in this exercise.

The Parser.java class in the parser package implements the parser itself. I decided to implement an SLR parser, which is based on a LR(0) technique, by following the guidelines given in the reference book "Compilers: principles, techniques and tools (2nd edition)" -- Aho, A.V., Sethi, R. & Ullman, J.D.. The most interesting parts of the project are here! :)

The Main.java class contains the main program which:

  1. 'Lexes' the specified input file (input.txt) to produce a token list. (Lexical Analyser)
  2. Parses the input using the stream of tokens produced by the lexer. (Parser)
  3. Outputs: the contents of the input.txt & grammar.txt files, the stream of tokens produced in step 1, the productions output by the parser and a visual representation of the final parse tree created.

Example output:

------------------
Input from .\src\input.txt : 
---
-4+0.5

------------------
Token stream produced by lexer: 
---
[< MINUS, - >, < INT, 4 >, < PLUS, + >, < FLOAT, 0.5 >]

------------------
Grammar specification (in ./src/grammar.txt) : 
---
Non-terminals: stmt,N,E,S,F,T,E',sign
Terminals: INT,FLOAT,PLUS,MINUS,MULT,COS,FACTORIAL,ฮต
Productions:
stmt -> N | E
N -> sign INT | sign FLOAT
sign -> MINUS | ฮต
E -> S E'
E' -> PLUS S E' | MINUS S E' | ฮต
S -> F MULT S | F
F -> COS F | T
T -> INT FACTORIAL | N

------------------
Reductions output by parser: 
---
sign -> MINUS
N -> sign INT
T -> N
F -> T
S -> F
sign -> ฮต
N -> sign FLOAT
T -> N
F -> T
S -> F
E' -> ฮต
E' -> PLUS S E'
E -> S E'
stmt -> E

------------------
Parse tree produced: 
---
stmt
โ””โ”€โ”€ E
    โ”œโ”€โ”€ E'
    โ”‚   โ”œโ”€โ”€ E'
    โ”‚   โ”‚   โ””โ”€โ”€ ฮต
    โ”‚   โ”œโ”€โ”€ S
    โ”‚   โ”‚   โ””โ”€โ”€ F
    โ”‚   โ”‚       โ””โ”€โ”€ T
    โ”‚   โ”‚           โ””โ”€โ”€ N
    โ”‚   โ”‚               โ”œโ”€โ”€ FLOAT
    โ”‚   โ”‚               โ”‚   โ””โ”€โ”€ 0.5
    โ”‚   โ”‚               โ””โ”€โ”€ sign
    โ”‚   โ”‚                   โ””โ”€โ”€ ฮต
    โ”‚   โ””โ”€โ”€ PLUS
    โ””โ”€โ”€ S
        โ””โ”€โ”€ F
            โ””โ”€โ”€ T
                โ””โ”€โ”€ N
                    โ”œโ”€โ”€ INT
                    โ”‚   โ””โ”€โ”€ 4
                    โ””โ”€โ”€ sign
                        โ””โ”€โ”€ MINUS

โ• Copyright Notice

Copyright ยฉ 2020 Alba Navarro Rosales. All rights reserved. Please do not copy or modify the design or software in this repository for any purpose other than with the express written permission of the author, neither claim it as your own. Do check this out, thanks! :)
โ˜๏ธ And remember- plagiarism is bad!

compilers_summerwork's People

Contributors

alba-nr 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.