Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
Introduce parser
Browse files Browse the repository at this point in the history
* Rename T to TOKEN

* Add support for comments in scanner

* Resolve EOF return in scanner

* Remove manually created token.h

* Refactor bminor.c

* Support --parse option in bminor.c

* Add tests for parser

* Add Makefile rules for parser

* Exclude token.h and grammar.tab.c from Git

* Create grammar.y

* Create parser.c and parser.h
  • Loading branch information
sghuang19 committed Nov 2, 2023
1 parent 00f692a commit aaab449
Show file tree
Hide file tree
Showing 27 changed files with 563 additions and 182 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ tags
bminor
*.o
lex.yy.c
token.h
grammar.tab.c

test/**/*.out
18 changes: 16 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bminor: bminor.c encoder.o scanner.o lex.yy.o
bminor: bminor.c encoder.o scanner.o lex.yy.o parser.o grammar.tab.o
gcc $^ -o $@

encoder.o: encoder.c
Expand All @@ -10,7 +10,17 @@ lex.yy.c: lex.yy.l
lex.yy.o: lex.yy.c
gcc -c $^ -o $@

scanner.o: scanner.c
scanner.o: scanner.c token.h
gcc -c $< -o $@

parser.o: parser.c
gcc -c $^ -o $@

grammar.tab.c token.h: grammar.y lex.yy.c
bison --defines=token.h $<
#bison -v --defines=token.h $<

grammar.tab.o: grammar.tab.c
gcc -c $^ -o $@

# Tests
Expand All @@ -20,8 +30,12 @@ test-encoder:
test-scanner:
./runtest.sh scanner

test-parser:
./runtest.sh parser

clean:
rm -f lex.yy.c
rm -f token.h grammar.tab.c grammar.output
rm -f *.o
rm -f ./test/*/*.bminor.out
rm -f bminor
93 changes: 41 additions & 52 deletions bminor.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "encoder.h"
#include "scanner.h"
#include "parser.h"

void usage(int exit_code)
{
Expand All @@ -13,61 +14,49 @@ void usage(int exit_code)

int main(int argc, char* argv[])
{
char* option, * filename;

if (argc == 1)
usage(1);
switch (argc)
{
case 1:
usage(EXIT_FAILURE);
break;
case 2:
if (strcmp(argv[1], "--help") == 0)
usage(EXIT_SUCCESS);
else usage(EXIT_FAILURE);
break;
case 3:
option = argv[1];
filename = argv[2];
break;
default:
fprintf(stderr, "Too many arguments.\n");
usage(EXIT_FAILURE);
break;
}

for (int i = 1; i < argc; i++)
if (strcmp(option, "--encode") == 0)
{
if (decode(filename) == 0)
return EXIT_SUCCESS;
}
else if (strcmp(option, "--scan") == 0)
{
if (scan(filename) == 0)
return EXIT_SUCCESS;
}
else if (strcmp(option, "--parse") == 0)
{
if (parse(filename) == 0)
return EXIT_SUCCESS;
}
else
{
if (argv[i][0] == '-')
{
if (strcmp(argv[i], "--help") == 0)
usage(0);
else if (strcmp(argv[i], "--encode") == 0)
{
const char* filename = argv[++i];
if (filename)
{
if (decode(filename) == 0)
return EXIT_SUCCESS;
else
{
fprintf(stderr, "Failed to decode file %s\n", filename);
return EXIT_FAILURE;
}
}
else
{
fprintf(stderr, "Missing filename to be encoded\n");
return EXIT_FAILURE;
}
}
else if (strcmp(argv[i], "--scan") == 0)
{
const char* filename = argv[++i];
if (filename)
{
if (scan(filename) == 0)
return EXIT_SUCCESS;
else
{
fprintf(stderr, "Failed to scan file %s\n", filename);
return EXIT_FAILURE;
}
}
else
{
fprintf(stderr, "Missing filename to be scanned\n");
return EXIT_FAILURE;
}
}
else
{
fprintf(stderr, "Unknown option '%s'\n", argv[i]);
usage(1);
}
}
fprintf(stderr, "Unknown option '%s'\n", option);
usage(EXIT_FAILURE);
}

return EXIT_SUCCESS;
fprintf(stderr, "Failed to %s file %s\n", option + 2, filename);
return EXIT_FAILURE;
}
Loading

0 comments on commit aaab449

Please sign in to comment.