Added initial commit of 'extra' OS assignment.

parent bdd7181f
CC=gcc
RM=rm -Rfv
LFLAGS= -lm
CFLAGS = -std=c99 -pedantic -Wall -Wextra -D_POSIX_SOURCE -D_GNU_SOURCE
ifdef DEBUG
CFLAGS += -ggdb -DDEBUG
else
CFLAGS += -g -O2
endif
all: shell
clean:
$(RM) shell *.o input.lex.* input.yacc.*
shell: input.yacc.o input.lex.o shell.o
$(CC) $(CFLAGS) $(LFLAGS) -o $@ $^
input.yacc.c: input.y
bison --debug --verbose -d -o $@ $<
input.yacc.h: input.yacc.c
input.lex.c: input.l
`which flex35 2>/dev/null || which flex 2>/dev/null` \
-C --header-file=input.lex.h -o $@ -d $<
input.lex.h: input.lex.c
%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<
input.lex.o: input.yacc.h input.l.h
input.yacc.o: input.lex.h
/*
* input.l
*
* Shell input lexer
*/
%{
// To get rid of the error "‘input’ defined but not used".
#define YY_NO_INPUT
//#define YY_USER_INIT yyset_debug(0);
#include <stdio.h>
#include "input.l.h"
#include "input.yacc.h"
int line_no = 1, column_no = 0;
extern int current_line(void){ return line_no; }
extern int current_column(void){ return column_no; }
extern void reset_line(void){ line_no = 1; }
extern void reset_column(void){ column_no = 0; }
%}
%option nounput
%option noyywrap
%x id
%x num
id [a-zA-Z_][[:alnum:]_.]*
num [0-9]+
wsp [\r\t ]
%%
/* Strings, like "abc" */
\"([^\\"]|\\.)*\" {
column_no += yyleng;
//yylval.sval = yytext;
return STRING;
}
/* Hexadecimal e.g. 0xf00fcafe */
/* Octal e.g. 0777 */
/* Numbers 0, 42, 9001 etc. */
0x[0-9a-f]+|{num} {
column_no += yyleng;
//yylval.sval = yytext;
return INTEGER;
}
/* Identifiers */
{id} {
//yylval.sval_annot.str = yytext;
//yylval.sval_annot.line = current_line();
//yylval.sval_annot.column = current_column();
column_no += yyleng;
return IDENTIFIER;
}
/* Math operators */
[-+,()] {
column_no++;
return *yytext;
}
#[^\n]+ { /* ignore comments */ }
\n {
line_no++;
reset_column();
return EOL;
}
/* White space */
{wsp} { column_no++; }
. {
column_no++;
fprintf(stderr, "error: illegal character '%c' (0%o)"
" at line %d, column %d\n", yytext[0], yytext[0],
line_no, column_no);
exit(EXIT_FAILURE);
}
%%
extern int current_line(void);
extern int current_column(void);
extern int yyparse();
extern void reset_line();
extern void reset_column();
%{
/*
* Shell input grammar (yacc/bison format)
*/
#include <stdio.h>
#include <string.h>
// #include "input.func.h"
#include "input.yacc.h"
#include "input.lex.h"
#include "input.l.h"
void yyerror(const char *str)
{
fflush(stdout);
fflush(stderr);
fprintf(stderr, "error: %s (line %d, column %d)\n", str,
current_line(), current_column());
exit(EXIT_FAILURE);
}
%}
%token COMMENT IDENTIFIER ARGUMENT STRING INTEGER
%token EOL
%left '-' '+'
%%
Start: /* empty */
| Command EOL
;
Command:
| IDENTIFIER
| IDENTIFIER Arguments
;
Arguments: ARGUMENT
| Arguments ' ' ARGUMENT
;
%%
/*
* Shell implementation using flex/bison.
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "input.yacc.h"
#include "input.lex.h"
#include "input.l.h"
int main(void) {
yyset_debug(1);
if( yyparse() ) {
fprintf(stderr, "Error: Parsing failed\n");
return EXIT_FAILURE;
}
yylex_destroy();
if( yyin )
fclose(yyin);
return EXIT_SUCCESS;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment