For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > August 2006 > Help regarding parser for my minishell









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help regarding parser for my minishell
bbanerji@yahoo.com

2006-08-16, 7:01 pm

Hi,
I am writing a simple shell(minishell). I am not sure which function
should I use to parse the commands typed at my shell prompl. How about
strtok? Need you suggestion.

Thanks.
Bani

Pascal Bourguignon

2006-08-16, 7:01 pm

bbanerji@yahoo.com writes:
> I am writing a simple shell(minishell). I am not sure which function
> should I use to parse the commands typed at my shell prompl. How about
> strtok? Need you suggestion.


Why not make it a nice gramatical language?
Then just use yacc or bison to parse it.

After all, we have more memory than when bourne shell was invented,
and you'll soon enough want to add control statement in addition to
the simple forking of commands.

while there is a file f in directory /var/spool/queue do
some-command f
rm f
done

--
__Pascal Bourguignon__ http://www.informatimago.com/
Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we. -- Georges W. Bush
Robert Harris

2006-08-16, 7:01 pm

bbanerji@yahoo.com wrote:
> Hi,
> I am writing a simple shell(minishell). I am not sure which function
> should I use to parse the commands typed at my shell prompl. How about
> strtok? Need you suggestion.
>
> Thanks.
> Bani


First (IMHO) you should define your shell command grammar.

Robert
bbanerji@yahoo.com

2006-08-16, 7:01 pm

Thanks for your suggestions.
I am a newbie in this area. Want to learn unix programming. My simple
shell should
able to execute commands like cd, ls cat and also handle input/output
redirection etc.


Bani

bbanerji@yahoo.com wrote:
> Hi,
> I am writing a simple shell(minishell). I am not sure which function
> should I use to parse the commands typed at my shell prompl. How about
> strtok? Need you suggestion.
>
> Thanks.
> Bani


Pascal Bourguignon

2006-08-16, 7:01 pm

bbanerji@yahoo.com writes:

> Thanks for your suggestions.
> I am a newbie in this area. Want to learn unix programming. My simple
> shell should
> able to execute commands like cd, ls cat and also handle input/output
> redirection etc.


So:

syntax:

start ::= command_list .
command_list ::= | command commad_list .
command ::= external_command command_sep.
external_command ::= path argument_list redirection_list .
path ::= atom .
argument_list ::= | argument argument_list .
argument ::= atom .
redirection_list ::= | redirection .
redirection ::= '<' atom | '>' atom | '>>' atom .

lexic:

atom = ([^<> \t]+|'([^']|\\|')*'|"([^"]|\\|\")*")*
command_sep = \n

You can feed (after translation to bison and flex syntax) the above
BNF add actions to each grammar rules (some C statements indicating
how to build the parse tree from the rule elements) and have your
parser in 30 minutes. Then implement the further atom unmangling
(removing ' " and \), and the fork/open/dup/exec/wait and you're done.
2 hours max.

Note that 80% is not unix programming, just plain C + bison/flex (or
yacc/lex). The only unix part is the fork/open/dup/exec/wait stuff,
which is really just one function.


Google for bison tutorial and flex tutorial.


--
__Pascal Bourguignon__ http://www.informatimago.com/

CAUTION: The mass of this product contains the energy equivalent of
85 million tons of TNT per net ounce of weight.
bbanerji@yahoo.com

2006-08-16, 7:01 pm

Thankyou very much. This info will help a lot.

Bani

Pascal Bourguignon wrote:
> bbanerji@yahoo.com writes:
>
>
> So:
>
> syntax:
>
> start ::= command_list .
> command_list ::= | command commad_list .
> command ::= external_command command_sep.
> external_command ::= path argument_list redirection_list .
> path ::= atom .
> argument_list ::= | argument argument_list .
> argument ::= atom .
> redirection_list ::= | redirection .
> redirection ::= '<' atom | '>' atom | '>>' atom .
>
> lexic:
>
> atom = ([^<> \t]+|'([^']|\\|')*'|"([^"]|\\|\")*")*
> command_sep = \n
>
> You can feed (after translation to bison and flex syntax) the above
> BNF add actions to each grammar rules (some C statements indicating
> how to build the parse tree from the rule elements) and have your
> parser in 30 minutes. Then implement the further atom unmangling
> (removing ' " and \), and the fork/open/dup/exec/wait and you're done.
> 2 hours max.
>
> Note that 80% is not unix programming, just plain C + bison/flex (or
> yacc/lex). The only unix part is the fork/open/dup/exec/wait stuff,
> which is really just one function.
>
>
> Google for bison tutorial and flex tutorial.
>
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> CAUTION: The mass of this product contains the energy equivalent of
> 85 million tons of TNT per net ounce of weight.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com