全部博文(230)
分类:
2009-05-30 23:13:49
if (Prev_Token = Tok_And and then Token = Tok_Then)
or else
(Prev_Token = Tok_Or and then Token = Tok_Else)
then
Scan;
end if;
function "+" (M1, M2 : Matrix) return Matrix;The lexical scanner is not always able to recognize whether a string is an operator, so it converts all strings that may denote an operator into an operator token. Semantic analysis may undo this choice if the context indicates that the construct is just a character string, as in the concatenation: "and" & " so what?".
-------------------------------The parser is not just a recognizer: it builds the syntax tree for the program. Thus, each subprogram is a function that returns the tree fragment corresponding to the construct that it recognizes. Each call to New_Node indicates the construction of a tree node corresponding to some non-terminal. The descendants of the node are constructed by calls to the corresponding functions. In this case, the assignment statement has two descendants: a name (the left-hand side of the assignment) and an expression. This function is called after the left-hand side and the assignment symbol have been processed, so the tree corresponding to the left-hand side is a parameter in the call (it has been built already). The right-hand side is recognized by P_Expression_No_Right_Paren (a variant of P_Expression that checks that the expression is not followed by a right parenthesis) followed by a semicolon. The assignment node is then returned to the caller.
-- 5.2 Assignment Statement --
-------------------------------
-- ASSIGNMENT_STATEMENT ::=
-- variable_NAME := EXPRESSION;
-- Error recovery: can raise Error_Resync
function P_Assignment_Statement (Lhs : Node_Id) return Node_Id is
Assign_Node : Node_Id;
begin
Assign_Node := New_Node (N_Assignment_Statement, Prev_Token_Ptr);
Set_Name (Assign_Node, LHS);
Set_Expression (Assign_Node, P_Expression_No_Right_Paren);
TF_Semicolon;
return Assign_Node;
end P_Assignment_Statement;