[Toybox] Would someone please explain what bash is doing here?

Chet Ramey chet.ramey at case.edu
Mon May 25 10:01:25 PDT 2020


On 5/25/20 6:39 AM, Rob Landley wrote:
> On 5/24/20 4:32 PM, Chet Ramey wrote:
>>> In new tab:
>>>   $ echo $LINENO\
>>>   > $LINENO
>>>   22
>>>   $ echo $LINENO
>>>   3
>>>
>>> It cares about the space.
>>
>> Of course it does!
> 
> It's not an "of course" for me. My code doesn't work like that.

OK. Bash uses tokens to communicate between the parser rules and the
lexical analyzer. The lexer does some lookahead, but it returns tokens
to the parser. The tokens are as POSIX describes them: operators, numbers,
or words. The words can be further differentiated.

> 
>> The space terminates the token!
> 
> Unquoted tokens self-terminate: ; or & or ( don't need a space before or after

These are operators. Words have to be terminated by metacharacters.

> them to be their own word. Therefore $LINENO can already _not_ be a function
> definition before you get to the \ because it started with something _other_
> than an unquoted "(".

If you look that far ahead and have a mechanism to communicate that back to
the parser. The bash parser just asks for a token and the lexer returns
one. Yacc works like that.

You have negative feedback between the lexer and parser. Bison only uses
positive feedback.


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet at case.edu    http://tiswww.cwru.edu/~chet/



More information about the Toybox mailing list