diff --git a/src/parser/Makefile b/src/parser/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..fa61c49cd6fb095a64808bcf97e8037c60995b46 --- /dev/null +++ b/src/parser/Makefile @@ -0,0 +1,19 @@ +LEXOR = motion_file +PARSER = motion_file + +all: $(PARSER) + +$(PARSER): $(PARSER).tab.c $(LEXOR).yy.c + gcc -o $(PARSER) $(PARSER).tab.c $(LEXOR).yy.c -ly -ll -lm + +$(PARSER).tab.c: $(PARSER).y + bison -vd $(PARSER).y + +$(LEXOR).yy.c: $(LEXOR).l + flex -o $(LEXOR).yy.c $(LEXOR).l + +clean: + rm -f $(LEXOR).yy.c + rm -f $(PARSER).output + rm -f $(PARSER).tab.* + rm -f $(PARSER) diff --git a/src/parser/motion_file.l b/src/parser/motion_file.l new file mode 100644 index 0000000000000000000000000000000000000000..a1c1447f5aff45451263d6aedd096fa9b43ab028 --- /dev/null +++ b/src/parser/motion_file.l @@ -0,0 +1,33 @@ +%{ +#include "motion_file.tab.h" +/* Here we recoginze the various letters that are part of your language. +*/ +void yyerror(char *s) +{ + printf("Parse error: %s\n",s); + // might as well halt now: + exit(-1); +} +%} +%% +[ \t] ; +type { return TYPE; } +version { return VERSION; } +enable { return ENABLE; } +motor_type { return MOTOR_TYPE; } +page_begin { return BEGIN_PAGE; } +name { return NAME; } +compliance { return COMPLIANCE; } +play_param { return PLAY_PARAMS; } +step { return STEP; } +page_end { return END_PAGE; } += { return EQUAL; } +[0-9]+\.[0-9]+ { yylval.fval=atof(yytext); return FLOAT; } +[0-9]+ { yylval.ival=atoi(yytext); return INT; } +[a-zA-Z0-9]+ { char *res=(char *)malloc(strlen(yytext)+1); + strcpy(res,yytext); + yylval.sval=res; + return STRING; } +. ; +%% + diff --git a/src/parser/motion_file.y b/src/parser/motion_file.y new file mode 100644 index 0000000000000000000000000000000000000000..0b56369335083b7f5e090b8a01db32210ebfd1f4 --- /dev/null +++ b/src/parser/motion_file.y @@ -0,0 +1,120 @@ +%{ +#include <stdio.h> + +int num_servos=0; +int num_pages=0; +int num_steps=0; +%} +%token TYPE VERSION ENABLE MOTOR_TYPE BEGIN_PAGE END_PAGE +%token NAME COMPLIANCE PLAY_PARAMS STEP +%token EQUAL + +%union { + int ival; + float fval; + char *sval; +} + +%token <ival> INT +%token <fval> FLOAT +%token <sval> STRING +%% +motion_file: type version enables motor_types pages { printf("Processed %d pages\n",num_pages); } + ; + +type: TYPE EQUAL STRING { printf("Type: %s\n",$3); } + ; + +version: VERSION EQUAL FLOAT { printf("version: %f\n",$3); } + ; + +enables: ENABLE EQUAL enable { printf("num servos (enable): %d\n",num_servos); + num_servos=0; } + ; + +enable: enable INT { num_servos++; } + | INT { num_servos++; } + ; + +motor_types: + | MOTOR_TYPE EQUAL motor_type { printf("num servos (motor_type): %d\n",num_servos); + num_servos=0; } + ; + +motor_type: motor_type INT { num_servos++; } + | INT { num_servos++; } + ; + +pages: pages page { num_pages++; } + | page { num_pages++; } + ; + +page: BEGIN_PAGE name compliances params steps END_PAGE { printf("page %d num steps %d\n",num_pages,num_steps); + num_steps=0; + num_servos=0; } + | BEGIN_PAGE name compliances params END_PAGE { printf("page %d with no steps\n",num_pages); + num_steps=0; + num_servos=0; } + ; + +name: NAME EQUAL { printf("page %d has no name\n",num_pages); } + | NAME EQUAL strings { printf("page %d name:\n", num_pages); } + ; + +strings: strings STRING + | STRING + ; + +compliances: COMPLIANCE EQUAL compliance { printf("page %d num servos (compliance): %d\n",num_pages,num_servos); + num_servos++; } + ; + +compliance: compliance INT { num_servos++; } + | INT { num_servos++; } + ; + +params: PLAY_PARAMS EQUAL INT INT INT FLOAT INT { printf("page %d next motion: %d\n",num_pages,$3); + printf("page %d exit motion: %d\n",num_pages,$4); + printf("page %d repetitions: %d\n",num_pages,$5); + printf("page %d speed rate: %f\n",num_pages,$6); + printf("page %d inertial force: %d\n",num_pages,$7); } + ; + +steps: steps step { num_steps++; } + | step { num_steps++; } + ; + + +step: STEP EQUAL angles FLOAT FLOAT { printf("page %d step %d pause time: %f\n",num_pages,num_steps,$4); + printf("page %d step %d step time: %f\n",num_pages,num_steps,$5); } + ; + +angles: angles INT + | INT + ; +%% +extern FILE *yyin; + +int main(int argc, char *argv[]) +{ + FILE *file; + + printf("parsing example.\n"); + if(argc!=2) + { + printf("A motion file mist be provided as an argument.\n"); + return -1; + } + file=fopen(argv[1],"r"); + if(file==NULL) + { + printf("Impossible to open the file %s\n",argv[1]); + return -1; + } + yyin=file; + do { + yyparse (); + }while(!feof(yyin)); + return 0; +} +