Skip to content

Commit

Permalink
:wq
Browse files Browse the repository at this point in the history
  • Loading branch information
Santosh Kalwar authored and Santosh Kalwar committed Dec 11, 2014
0 parents commit a91deff
Show file tree
Hide file tree
Showing 10 changed files with 1,898 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
Empty file added .localized
Empty file.
Binary file added LCI_0331927/.DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions LCI_0331927/LICENSE.rtf
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.}
26 changes: 26 additions & 0 deletions LCI_0331927/Makefile
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)
35 changes: 35 additions & 0 deletions LCI_0331927/ReadMe.txt
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)





















51 changes: 51 additions & 0 deletions LCI_0331927/prj.h
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
155 changes: 155 additions & 0 deletions LCI_0331927/prj.l
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
*/
Loading

0 comments on commit a91deff

Please sign in to comment.