Skip to content
Snippets Groups Projects
Commit 16f85546 authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Added a C version of the motion file parser.

parent f8ec54b6
No related branches found
No related tags found
No related merge requests found
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)
%{
#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; }
. ;
%%
%{
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment