Skip to content

Commit

Permalink
lab codes
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhakaran9397 committed Mar 10, 2017
1 parent cee00b7 commit c27bc7e
Show file tree
Hide file tree
Showing 53 changed files with 2,935 additions and 0 deletions.
Binary file added LAB/03-02-17/lexicalerrors
Binary file not shown.
76 changes: 76 additions & 0 deletions LAB/03-02-17/lexicalerrors.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
%{

#include <stdio.h>

enum {
NOTHING=0,
DATATYPE,
VARIABLE,
TERMINATOR
};

int seen = NOTHING;
int check = 0;
int line = 0;
int offset = 0;

%}

datatype int|float|char
variable [a-zA-Z_]*_[a-zA-Z0-9_]*
terminator ;
character .
eol \n

%%

{datatype} {
offset += yyleng;
check = 1;
seen = DATATYPE;
}

{variable} {
if(seen == DATATYPE || seen == VARIABLE) {
offset += yyleng;
seen = VARIABLE;
}
else {
seen = NOTHING;
}
}

{terminator} {
if(seen == VARIABLE) {
offset += yyleng;
seen = TERMINATOR;
}
else {
seen = NOTHING;
}
}

{eol} {
++line;
if(check && seen != TERMINATOR) {
printf("Error at %d in line:[%d]\n", offset, line);
seen = NOTHING;
check = 0;
}
offset = 0;
}

{character} {
++offset;
}

%%

int main(int argc, char *argv[])
{
yyin = fopen(argv[1], "r");
yylex();
fclose(yyin);
return 0;
}

Binary file added LAB/03-02-17/syntax
Binary file not shown.
35 changes: 35 additions & 0 deletions LAB/03-02-17/syntax.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
%option yylineno
%option noyywrap

type int|float|char
id [a-zA-Z_][a-zA-Z_0-9]*
ws [ \t]+
arr "["[0-9]+"]"

%s WS
%s ID
%s ARR_COMMA_SEMI
%s ERROR

%{
int pos = 0;
%}

%%
<INITIAL>{type} { BEGIN WS; pos+=yyleng; }
<INITIAL>{ws} { pos+=yyleng; }
<WS>{ws} { BEGIN ID; pos+=yyleng; }
<ID>{id} { BEGIN ARR_COMMA_SEMI; pos+=yyleng; }
<ARR_COMMA_SEMI>{arr} { pos+=yyleng; }
<ARR_COMMA_SEMI>, { BEGIN ID; pos+=yyleng; }
<ARR_COMMA_SEMI>; { BEGIN INITIAL; pos+=yyleng; }
<ERROR>; { BEGIN INITIAL; pos+=yyleng; }
<ERROR>. { pos+=yyleng; }
\n { pos = 0; BEGIN INITIAL; }
. { printf("Error:%d:%d\n", yylineno, pos+1); BEGIN ERROR; yyless(0); }
%%

int main(void)
{
return yylex();
}
Binary file added LAB/03-02-17/syntaxerrors
Binary file not shown.
78 changes: 78 additions & 0 deletions LAB/03-02-17/syntaxerrors.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
%{

#include <stdio.h>

enum {
NOTHING=0,
DATATYPE,
VARIABLE,
TERMINATOR
};

int seen = NOTHING;
int check = 0;
int line = 0;
int offset = 0;
int function = 0;

%}

datatype int|float|char
variable [a-zA-Z_][a-zA-Z0-9_]*
subscript (\[[0-9]*\])+
terminator ;
character .
eol \n

%%

{datatype} {
offset += yyleng;
check = 1;
seen = DATATYPE;
}

{variable} {
if(seen == DATATYPE || seen == VARIABLE) {
offset += yyleng;
seen = VARIABLE;
}
else {
seen = NOTHING;
}
}

{terminator} {
if(seen == VARIABLE) {
offset += yyleng;
seen = TERMINATOR;
}
else {
seen = NOTHING;
}
}

{eol} {
++line;
if(check && seen != TERMINATOR) {
printf("Error at %d in line:[%d]\n", offset, line);
seen = NOTHING;
check = 0;
}
offset = 0;
}

{character} {
++offset;
}

%%

int main(int argc, char *argv[])
{
yyin = fopen(argv[1], "r");
yylex();
fclose(yyin);
return 0;
}

Binary file added LAB/03-03-17/expr/a.out
Binary file not shown.
Binary file added LAB/03-03-17/expr/expr
Binary file not shown.
20 changes: 20 additions & 0 deletions LAB/03-03-17/expr/expr.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%{
#include "expr.yacc.h"
%}

WS [ \t]
UOPT "++"|"--"
BOPT "+"|"-"|"/"|"*"|"%"
LOPT "&"|"|"|"^"
ID [a-z]+
NUM [0-9]+

%%
{BOPT}{WS}* { puts("(BOPT)"); return BOPT; }
{UOPT}{WS}* { puts("(UOPT)"); return UOPT; }
{LOPT}{WS}* { puts("(LOPT)"); return LOPT; }
{ID}{WS}* { puts("(ID)"); return ID; }
{NUM}{WS}* { puts("(NUM)"); return NUM; }
\n { return *yytext; }
. { return *yytext; }
%%
40 changes: 40 additions & 0 deletions LAB/03-03-17/expr/expr.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
%{
#include <bits/stdc++.h>
extern int yylex();
extern int yyerror(const char*);
%}

%token UOPT BOPT LOPT ID NUM

%%
all: expr all
|
;

expr: '\n' { puts("[nl]"); }
| unary { puts("[**[unary]**]"); }
| binary { puts("[**[binary]**]"); }
| logical { puts("[**[logical]**]"); }
| opand { puts("[opand]"); }
;
unary: UOPT opand { puts("[UOPT][opand]"); }
| opand UOPT { puts("[opand][UOPT]"); }
;
binary: expr BOPT expr { puts("[expr][BOPT][expr]"); }
;
logical: expr LOPT expr { puts("[expr][LOPT][expr]"); }
;
opand: ID { puts("[ID]"); }
| NUM { puts("[NUM]"); }
;
%%

int yyerror(const char* s)
{
fprintf(stderr, "%s\n", s);
}

int main(void)
{
yyparse();
}
7 changes: 7 additions & 0 deletions LAB/03-03-17/expr/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>

int main()
{
int a, b;
int c = a+++++b;
}
1 change: 1 addition & 0 deletions LAB/03-03-17/ifelse/g.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if(a+b){a+c}
Binary file added LAB/03-03-17/ifelse/ifelse
Binary file not shown.
22 changes: 22 additions & 0 deletions LAB/03-03-17/ifelse/ifelse.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
%{
#include "ifelse.yacc.h"
%}

WS [ \t\n]
UOPT "++"|"--"
BOPT "+"|"-"|"/"|"*"|"%"
LOPT "&"|"|"|"^"
ID [a-z]+
NUM [0-9]+

%%
if return IF;
else return ELSE;
{BOPT} return BOPT;
{UOPT} return UOPT;
{LOPT} return LOPT;
{ID} return ID;
{NUM} return NUM;
{WS} ;
. return *yytext;
%%
46 changes: 46 additions & 0 deletions LAB/03-03-17/ifelse/ifelse.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
%{
#include <bits/stdc++.h>
extern int yylex();
extern int yyerror(const char*);
%}

%token UOPT BOPT LOPT ID NUM IF ELSE

%%

stmt : exprs { puts("[exprs]"); }
| '{' stmt '}' { puts("[stmt]"); }
| ifstmt { puts("[ifsmt]"); }
;
ifstmt : IF '(' expr ')' stmt { puts("[if]"); }
| ifstmt ELSE stmt { puts("[ifelse]"); }
;
exprs : exprs expr ';' { puts("[exprs expr ;]"); }
|
;
expr : unary { puts("[**[unary]**]"); }
| binary { puts("[**[binary]**]"); }
| logical { puts("[**[logical]**]"); }
| opand { puts("[opand]"); }
;
unary : UOPT expr { puts("[UOPT][expr]"); }
| expr UOPT { puts("[expr][UOPT]"); }
;
binary : expr BOPT expr { puts("[expr][BOPT][expr]"); }
;
logical : expr LOPT expr { puts("[expr][LOPT][expr]"); }
;
opand : ID { puts("[ID]"); }
| NUM { puts("[NUM]"); }
;
%%

int yyerror(const char* s)
{
fprintf(stderr, "%s\n", s);
}

int main(void)
{
yyparse();
}
Binary file added LAB/06-01-17/dec
Binary file not shown.
Loading

0 comments on commit c27bc7e

Please sign in to comment.