Skip to content

Raghav714/RDDS-online-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RDDS-online-compiler

The compiler is build from scratch with module :-
• Lexical analyser
• Parser
• Intermediate Code Generator
• Error handling
• Symbol table

LEXICAL ANALYSER AND SCANNER

• First program is run on the source file and then the code is broken into the list of distinct lines.
• at the start of each non-whitespace character, the frst character is used to determine the type of the token to expect.
• The token is returned if the type is matched without any issue Otherwise, a scanner warning is thrown.
• The scanner warnings are never fatal, though syntactically the tokens returned may cause a parser error.
• For instance, if a string literal has no end quote a warning will be thrown and a quote will be assumed at the end of the line.

PARSER

• The left recursive from the grammar is removed.
• Type-checking is performed in expressions by returning the types from the expression tree functions and evaluating types for compatibility if an operation is performed.
• Parser resync points are used throughout the compiler to continue parsing if an error is encountered without propagating spurious error messages.
• Note that once a fatal error or any kind is encountered, code will no longer be generated.

INTERMEDIATE CODE GENERATOR

• Memory and registers for the operation of the program are defned and used as 32-bit integer arrays.
• All non-integer types present in the program are cast as integers for storage in the memory spaces.
• To ensure that pointers are 32-bit and may be cast to integer without issue, the gcc compiler fag -m32 is used.
• Future improvements could be made to "push back" register allocation to the frst register (R[0]) at the end of each scope.

LANGUAGE SYNTAXPROGRAM BODY

the program <name> is
define <declaration>
body
<statement>
fnish program

FUNCTION DEFINITION

function <function name>(<datatype> <variable> in, <datatype> <variable> out) is
<declaration>
body
<statement>finsh functione;

IF ELSE STATEMENT

if (<condition>) then
<statement>
else
<statement>
finsh if

LOOP

for (counter = counter + 1e counter <= num_loops)<\br> <statement><\br> fnish for<\br>

FUNCTION CALL

<function name>(<parameter to pass>,<return value>)

DATA TYPE

• int – Integer
• bool – Boolean
• str – String
• foat – Float

FUNCTION

• putstr("Enter 1st Integer:")
• getint(variable)
• putint(result)
• getstr(“enter string”)
• getbool(testbool)
• putbool(testbool)
• getfoat(testfoat)
• putfoat(testfoat)

GRAMMAR

<program> ::= <program_header> <program_body>
<program_header> ::= ‘ the’ 'program' <identifier> 'is'
<program_body> ::= ‘ define’( <declaration> ';' )* 'body' ( <statement> ';' )* 'finish' 'program'
<declaration> ::= [ 'global' ] <procedure_declaration> [ 'global' ] <variable_declaration>
<variable_declaration> ::=<type_mark> <identifier> [ '[' <array_size> ']' ]
<type_mark> ::= 'int’' | 'float' | 'bool' | 'str'
<procedure_declaration> ::= <procedure_header> <procedure_body>
<procedure_header> ::='function' <identifier> '(' [ <parameter_list> ] ')' ’is’
<procedure_body> ::= ( <declaration> ';' )* ‘ body' ( <statement ';' )* 'finish’ 'function'
<parameter_list> ::= <parameter> ',' <parameter_list> |<parameter>
<parameter> ::= <variable_declaration> ( 'in' | 'out' )
<statement> ::= <assignment_statement> | <if_statement> | <loop_statement> | <return_statement> | <procedure_call>
<assignment_statement> ::= <destination> '=' <expression>
<if_statement> ::= 'if' '(' <expression> ')' 'then' ( <statement> ';' )+ [ 'else' ( <statement> ';' )+ ] 'finish' 'if'
<loop_statement> ::= 'for' '(' <assignment_statement> ';' <expression> ')' ( <statement> ';' )* 'finish' 'for'
<procedure_call> ::= <identifier> '(' [ <argument_list> ] ')'
<argument_list> ::= <expression> ',' <argument_list> |<expression>
<destination> ::= <identifier> [ '[' <expression> ']' ]
<expression> ::= <expression> '&' <arith_op> | <expression> '|' <arith_op> | [ 'not' ] <arith_op>
<arith_op> ::= <arith_op> '+' <relation> | <arith_op> '-' <relation> |<relation>
<relation> ::= <relation> '<' <term> | <relation> '>' <term> | <relation> '>=' <term> | <relation> '<=' <term> |<relation> '==' <term> |<relation> '!=' <term> |<term>
<term> ::= <term> '' <factor> | <term> '/' <factor> | <factor>
<factor> ::= '(' <expression> ')' | [ '-' ] <name> | [ '-' ] <number> | <string> | 'true' | 'false' |
<name> ::= <identifier> [ '[' <expression> ']' ]
<identifier> ::= [a-zA-Z][a-zA-Z0-9_]

<number> ::= [0-9][0-9_][.[0-9_]]?
<string> ::= "[a-zA-Z0-9 _,;:.']*"

GUI

compiler1 compiler2

STEPS to run the program

$ sudo apt update
$ sudo apt install apache2
$ sudo apt-get install python3<\br> $ sudo systemctl stop apache2.service
$ sudo systemctl start apache2.service
$ sudo systemctl enable apache2.service
$ sudo apt install php7.2 libapache2-mod-php7.2 php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-soap php7.2-gd php7.2-xml php7.2-intl php7.2-mysql php7.2-cli php7.2-zip php7.2-curl
Copy the HTML, PHP, CSS, input.txt and test.src file in /var/www/html/ and type the localhost/index1.php in browser.
Edit the Path of the file compiler.py in index1.php
if the program doesnot work then run $ sudo chmod 777 * in /var/www/html/
if you wish to make RDDS as a command then open the bashrc file(sudo gedit ~/.bashrc) and write the command alias RDDS ='python3 /home/raghav/Downloads/compiler/2compiler/compiler.py -o OUT' in this you need to replace the path of compiler.py file.
use the command RDDS rdds_test/simple_add.src and ./a.out to see the output