-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Santosh Kalwar
authored and
Santosh Kalwar
committed
Dec 11, 2014
0 parents
commit a91deff
Showing
10 changed files
with
1,898 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{\rtf1\ansi\ansicpg1252\cocoartf1343\cocoasubrtf160 | ||
{\fonttbl\f0\fnil\fcharset0 Tahoma;} | ||
{\colortbl;\red255\green255\blue255;\red38\green38\blue38;} | ||
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0 | ||
\deftab720 | ||
\pard\pardeftab720 | ||
|
||
\f0\fs28 \cf0 \cb1 \expnd0\expndtw0\kerning0 | ||
Copyright (c) 2008 Santosh Kalwar - All Rights Reserved\ | ||
\ | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\ | ||
\ | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
CC = gcc | ||
LIBS = -lfl # Linux | ||
LEX = flex -I | ||
|
||
BISON = bison -d | ||
CFLAGS = -DYYDEBUG=1 -g | ||
|
||
DEP1=prj.tab.c prj.yy.c | ||
EXE1=prj | ||
|
||
.SUFFIXES: # Delete the default suffixes | ||
.SUFFIXES: .y .tab.c .l .yy.c # Define our suffix list | ||
|
||
.y.tab.c: | ||
$(BISON) $< | ||
|
||
.l.yy.c: | ||
$(LEX) -i -o$@ $< | ||
|
||
all: $(EXE1) prj.h | ||
|
||
$(EXE1): $(DEP1) prj.h | ||
$(CC) -w -o $(EXE1) $(DEP1) $(LIBS) $(CFLAGS) | ||
|
||
clean: | ||
rm -f *.tab.h core $(DEP1) *.o $(EXE1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Copyright (c) 2008 Santosh Kalwar - All Rights Reserved | ||
|
||
Reference or help taken from | ||
https://www.it.lut.fi/kurssit/07-08/CT20A6400/projects.html | ||
https://www.it.lut.fi/kurssit/07-08/CT20A6400/project-english.html | ||
https://www.stanford.edu/class/cs143/ | ||
https://www.it.lut.fi/kurssit/07-08/CT20A6400/exercises.html | ||
|
||
|
||
Instruction on how to execute: | ||
|
||
1. Run the Make file | ||
2. Type ./prj filename.p (put in the argument the pascal file to make it executable) | ||
3. Type ./filename (Executes the program) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#ifndef _PRJ | ||
#define _PRJ | ||
|
||
#define TABSIZE 100 /* size of the tables */ | ||
|
||
|
||
//Structure for numbers, varibles | ||
//if identifier is 0 or constant 1 | ||
typedef struct | ||
{ | ||
// type = 0: name of variable | ||
// type = 1: integer | ||
int type; | ||
union | ||
{ | ||
char *symbol; | ||
int number; | ||
} argument; | ||
char *strvalue; | ||
} argumentnode; | ||
|
||
|
||
//Structure needed in while loop | ||
//When using TAC | ||
|
||
typedef struct | ||
{ | ||
char *beginlabel; | ||
char *endlabel; | ||
} labelnode; | ||
|
||
// symbol table | ||
char *symtable[TABSIZE]; | ||
char *repeatlabels[TABSIZE]; | ||
char *iflabels[TABSIZE]; | ||
char *thenlabels[TABSIZE]; | ||
char *strings[TABSIZE]; | ||
|
||
// Function declarations | ||
void yyerror(char *); | ||
int yylex(void); | ||
|
||
char *symlook(char*); | ||
char *newtemp(void); | ||
char *addstr(char*); | ||
char *newlabel(void); | ||
char *getlastlabel(void); | ||
char *getstrarg(argumentnode); | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
%{ | ||
/* Copyright (C) 2008 Santosh Kalwar - All Rights Reserved | ||
* You may use, distribute and modify this code under the | ||
* terms of the MIT license, which unfortunately won't be | ||
* written for another century. | ||
/* Credits and reference from | ||
* https://www.it.lut.fi/kurssit/07-08/CT20A6400/projects.html | ||
* https://www.it.lut.fi/kurssit/07-08/CT20A6400/project-english.html | ||
* https://www.stanford.edu/class/cs143/ | ||
* https://www.it.lut.fi/kurssit/07-08/CT20A6400/exercises.html | ||
* https://www.gnu-pascal.de/gpc/index.html | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include "prj.h" | ||
#include "prj.tab.h" | ||
|
||
int file_num; | ||
int file_num_max; | ||
char **files; | ||
extern int errno; | ||
int linenumber=1; | ||
|
||
%} | ||
|
||
%x COMMENT1 | ||
%x COMMENT2 | ||
|
||
%% | ||
"(*" BEGIN COMMENT1; | ||
"*)" {printf(" Unmatching comment marks{\n"); //Pascal's Comment | ||
yyterminate();} | ||
"{" BEGIN COMMENT2; | ||
"}" {printf(" Unmatching comment marks{\n"); | ||
yyterminate();} | ||
|
||
[ \t]+ ;//whitespaces is required to separate. | ||
|
||
":=" {return T_ASSIGN;} | ||
"+" {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
"-" {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
"*" {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
"DIV" {//printf("%s :%d\n",yytext,linenumber);//Basic integer division | ||
return T_DIV;} | ||
|
||
"=" {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
">" {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
"<" {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
">=" {//printf("%s :%d\n",yytext,linenumber); | ||
return T_GE;} | ||
"<=" {//printf("%s :%d\n",yytext,linenumber); | ||
return T_LE;} | ||
"<>" {//printf("%s :%d\n",yytext,linenumber); | ||
return T_NE;} | ||
"(" {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
")" {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
";" {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
":" {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
"," {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
"." {//printf("%s :%d\n",yytext,linenumber); | ||
return *yytext;} | ||
|
||
program {return T_PROGRAM;} | ||
var {return T_VAR;} | ||
begin {return T_BEGIN;} | ||
end {return T_END;} | ||
integer {return T_INTEGER;} | ||
if {return T_IF;} | ||
then {return T_THEN;} | ||
else {return T_ELSE;} | ||
while {return T_WHILE;} | ||
do {return T_DO;} | ||
repeat {return T_REPEAT;} | ||
until {return T_UNTIL;} | ||
write {return T_WRITE;} | ||
writeln {return T_WRITELN;} | ||
|
||
[a-z]([a-z]|[0-9]|_)* {//variables | ||
yylval.arg.type = 0; | ||
yylval.arg.argument.symbol = symlook(yytext); | ||
//yylval = symlook(yytext); | ||
return T_VARIABLE;} | ||
[0-9]+ {//integer | ||
yylval.arg.type = 1; | ||
yylval.arg.argument.number = atoi(yytext); | ||
//yylval = symlook(yytext); | ||
return T_INTEGER_NUM;} | ||
\'([^\'\n]|\\n|\\\')*\' {//string | ||
yylval.arg.type = 2; | ||
yylval.arg.argument.symbol = addstr(yytext); | ||
return T_STRING;} | ||
\n linenumber++; | ||
. printf("%s :%d unrecognized\n",yytext,linenumber); | ||
|
||
<COMMENT1>"*)" BEGIN INITIAL; | ||
<COMMENT1>. ; | ||
<COMMENT1>\n linenumber++; | ||
<COMMENT1>"(*" {printf(" Comments inside a comment not accepted(*!\n"); | ||
yyterminate(); } | ||
<COMMENT2>"}" BEGIN INITIAL; | ||
<COMMENT2>. ; | ||
<COMMENT2>\n linenumber++; | ||
|
||
%% | ||
|
||
/* | ||
//Main Program to take arguments in the command prompt. | ||
int main(int argc, char *argv[]) { | ||
printf("****************************************"); | ||
printf("\nWelcome to Pascal's Lexer Part I" ); | ||
printf("\nDisplay's line number for tokens" ); | ||
printf("\n****************************************\n"); | ||
file_num=1; | ||
fileno_max = argc; | ||
files = argv; | ||
if ( argc > 1 ) { | ||
if ( (yyin = fopen(argv[file_num],"r")) == 0 ) { | ||
perror(argv[file_num]); | ||
exit(1); | ||
} | ||
} | ||
while( yylex() ) | ||
; | ||
return 0; | ||
} | ||
int yywrap() { | ||
fclose(yyin); | ||
if ( ++file_num < fileno_max ) { | ||
if ( (yyin = fopen(files[file_num],"r")) == 0 ) { | ||
perror(files[file_num]); | ||
exit(1); | ||
} | ||
return 0; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
// This is the end of the lexer part of the code. | ||
// Comments and feedback at santosh (dot) kalwar (at) lut (dot) fi | ||
*/ |
Oops, something went wrong.