0
votes

I have a project in my university to make a mini shell with the C language. For parse the command line I use the tools: lex and yacc. But I want parse the line. When token is detected add argument in my struct command and when I find a newline exit yyparse for execute command. After print prompt and relauch parse ... My lex file:

Chiffre [0-9]
Lettre [a-zA-Z]
Alphanum ({Chiffre}{Lettre})+

%{

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "parser.tab.h"

%}
%%
[ \t]+ {}
"\n" {
   return '\n';
}
"||" {
 return OR;
}
"|" {
  return PIPE;
}
"&&" {
  return AND;
 }
"&" {
  return AMPERSAND;
}

"2>" {
  return ERR_GREAT;
}

">&" {
  return GREAT_AMP;
}

"2>>" {
 return ERR_GREAT_GREAT;
}
">>&" {
  return GREAT_GREAT_AMP;
}
">>" {
 return GREAT_GREAT;
}

">" {
 return GREAT;
}

"<" {
  return LESS;
}

";" {
  return SEMICOLON;
}

"exit" {
   return EXIT;
}

({Lettre}_){1}({Alphanum}_) {
    return IDENTIFIER;
}
[^ \t|><&\;][^ \t|><&\;]* {
 return WORD;
}
%%
 int yywrap(void){return 1;}

I try use YYACCEPT for exit yyparse but isn't working. The yacc file:

%{
#include <stdio.h>
void yyerror(const char *s);
extern int yylex();
extern char* yytext;
%}
%token PIPE AND OR AMPERSAND BLANK WORD IDENTIFIER GREAT GREAT_GREAT LESS ERR_GREAT ERR_GREAT_GREAT GREAT_AMP GREAT_GREAT_AMP SEMICOLON EXIT
%start cmd_lists
%union { char str[256]; int val; }
%error-verbose
%% 
list_arg: list_arg WORD | /*empty*/;
cmd_args: WORD list_arg io_list;
pipeline: pipeline PIPE cmd_args | pipeline SEMICOLON cmd_args | pipeline AND cmd_args | pipeline OR cmd_args | cmd_args ;
io: GREAT WORD | GREAT_GREAT WORD | LESS WORD | ERR_GREAT WORD | ERR_GREAT_GREAT WORD | GREAT_AMP WORD | GREAT_GREAT_AMP WORD;
io_list: io_list io | /*empty*/ ;
background: AMPERSAND | /*empty*/ ;
command: pipeline background '\n'{
        printf("cmd newline\n");
        YYACCEPT;
      }
     |'\n'{
        printf("newline\n");
       YYACCEPT;
     }
     |EXIT '\n'{
       printf("exit newline\n");
       YYACCEPT;
     }
     ;

   cmd_lists: cmd_lists command | ;
   %%

   void yyerror(const char *s){
       fprintf(stderr,"yyerror : erreur : %s.\n",s);
   }

Thank you for your help.

1

1 Answers

0
votes
cmd_lists: cmd_lists command | ;

This appears to be the terminal symbol, although they are normally put at the top, not at the bottom. Anyway, this grammar says that cmd_list can contain zero or more command items, so the parser is behaving correctly by getting ready to accept another one.

If you don't want that, remove the cmd_list production.