Top Banner
S B Syntax analysis How ScriptBasic performs Syntax analysis
24

SB Syntax analysis How ScriptBasic performs Syntax analysis.

Jan 18, 2016

Download

Documents

Valerie Greer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B

Syntax analysis

How ScriptBasic performs Syntax analysis

Page 2: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Contents

• What is the role of syntax analysis• What is the assumed syntax of a

program• What tables drive syntax analysis• Creating the tables• Pseudo terminals

Page 3: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Who this presentation is for

• Curious (why things happen?)• Want to learn and understand how

ScriptBasic works• Want to modify ScriptBasicNOT for those, who just• want to program in scriba

Page 4: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Role of syntax analysis

• Reader• Lexer• SYNTAX ANALYSIS• Builder• Execution

Page 5: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B General purpose

• The syntax analyzer is a general purpose table driven syntax analyzer that could be used for other syntaxes not hard coded for ScriptBasic

• The first definition that fits is used.

• Easy to maintain but slower than special purpose analyzer

Page 6: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Syntax assumed

• Program is series of lines• Lines are (usually) terminated by new

line• A line is series of terminal and pseudo-

terminal symbols

• No block of code { } or BEGIN END– handled by go|come_forward|backward pseudo

terminals

• Predefined pseudo terminal symbols, like ‘expression’

Page 7: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Tables defining syntax

• Binary operators• Unary operators• Built-in functions• Commands

– List of commands in the order syntax analyzer checks them against the lines

Page 8: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Creating the tables

• syntax.def is a readable format• syntaxer.pl creates the syntax.c• headerer.pl creates the syntax.h

files.

Page 9: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Terminals and pseudo terminals

• ‘module’ is a terminal symbol (keyword)

• nl is terminal symbol (character)• float is terminal symbol• lval, expression are pseudo

terminals– can create complex code (one way

street with dead end effect!)– can have side effect

Page 10: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Code generated

• Code is array of nodes• A node contains OpCode

– list of command parameters (each is a node)

– operands list (operators or function)– value (constant node)– Serial number (variable node)– start point and arguments (user defined

function)– car and cdr values for list head nodes

• See builder.c struct _cNODE;

Page 11: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Example

• GOTO: 'goto' label nl

• OpCode: CMD_GOTO– Parameter.CommandArgument.Argument.pNode

contains the node id of the line for label

Page 12: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Example

• ELSIF: 'else' 'if' * expression 'then' come_forward(IF) go_forward(IF) nl

• OpCode: CMD_ELSIF– Parameter.CommandArgument.Argument.pNode

contains the node id of the expression– Parameter.CommandArgument.next next

paremeter node:• Parameter.CommandArgument.Argument.pNode

where to go if expression fails

Page 13: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Pseudo terminals 1/10

• nl• tab

These are the simplest pseudo-terminal symbols, because they are real terminals, just hard to write into a text file.

Page 14: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Pseudo terminals 2/10

• expression• expression_list

Handles an expression or a comma separated list of expressions and

creates nodes.

OWSWDE

Page 15: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Pseudo terminals 3/10

• string• integer• float

These are simple terminals.

Page 16: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Pseudo terminals 4/10

• symbol a symbol (in name space)• absolute_symbol an absolute symbol• name_space an absolute symbol that sets

the name space (no code)• end_name_space

end a name space (no code)

Page 17: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Pseudo terminals 5/10

• lval a left value• lval_list a left value list separated

bycommas

• local local variable• local_list list of local variables

Page 18: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Pseudo terminals 6/10

• function a function name where it isdefined

• thisfn the name of the currentlydefined function or

procedure• arg_num placeholder to store the

number of arguments• local_start starts a local scope• local_end ends of local scope

Page 19: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Pseudo terminals 7/10

• label a label used in goto• label_def a label when defined

Page 20: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Pseudo terminals 8/10

• cname constant name• cvalconstant value corresponding

to constant nameThese do not generate code, have only

side effects defining a constant. (cval defines a constant for the name last

appeared for cname)

Page 21: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Pseudo terminals 9/10

• go_back creates code• go_forward creates code• come_back has side effect• come_forward has side effect

These are used instead of code bracketing. There is a jump stack

where node pointers are pushed and taken from.

Page 22: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Pseudo terminals 10/10

• * star character OWSWDE• noexec no code is generated from

the line

Page 23: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B Special commands

• All lines are analyzed by the C function

ex_IsCommandThisexcept those defining special analysis function

• CALL/CALL (ex_IsCommandCALL)• OPEN/OPEN (ex_IsCommandOPEN)

Page 24: SB Syntax analysis How ScriptBasic performs Syntax analysis.

S B

Thank you for listening