-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsyntax2.c
129 lines (104 loc) · 4.11 KB
/
syntax2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/****************************************************************/
/* */
/* Program 6-2: Pascal Syntax Checker II */
/* */
/* Check the syntax of Pascal declarations and */
/* statements. Perform type checking. */
/* */
/* FILE: syntax2.c */
/* */
/* REQUIRES: Modules parser, symbol table, scanner, */
/* error */
/* */
/* USAGE: syntax2 sourcefile */
/* */
/* sourcefile name of source file containing */
/* statements to be checked */
/* */
/* Copyright (c) 1991 by Ronald Mak */
/* For instructional purposes only. No warranties. */
/* */
/****************************************************************/
#include <stdio.h>
#include "common.h"
#include "error.h"
#include "scanner.h"
#include "parser.h"
/*--------------------------------------------------------------*/
/* Externals */
/*--------------------------------------------------------------*/
extern TOKEN_CODE token;
extern int line_number, error_count;
extern TYPE_STRUCT dummy_type;
/*--------------------------------------------------------------*/
/* Globals */
/*--------------------------------------------------------------*/
char buffer[MAX_PRINT_LINE_LENGTH];
/*--------------------------------------------------------------*/
/* main Initialize the scanner and call the */
/* statement routine. */
/*--------------------------------------------------------------*/
main(argc, argv)
int argc;
char *argv[];
{
SYMTAB_NODE_PTR program_idp; /* artificial program id */
/*
-- Initialize the scanner and the symbol table.
*/
init_scanner(argv[1]);
init_symtab();
/*
-- Create an artifical program id node.
*/
program_idp = alloc_struct(SYMTAB_NODE);
program_idp->defn.key = PROG_DEFN;
program_idp->defn.info.routine.key = DECLARED;
program_idp->defn.info.routine.parm_count = 0;
program_idp->defn.info.routine.total_parm_size = 0;
program_idp->defn.info.routine.total_local_size = 0;
program_idp->typep = &dummy_type;
program_idp->label_index = 0;
/*
-- Parse a block.
*/
get_token();
block(program_idp);
/*
-- Look for the end of file.
*/
while (token != END_OF_FILE) {
error(UNEXPECTED_TOKEN);
get_token();
}
quit_scanner();
/*
-- Print the parser's summary.
*/
print_line("\n");
print_line("\n");
sprintf(buffer, "%20d Source lines.\n", line_number);
print_line(buffer);
sprintf(buffer, "%20d Source errors.\n", error_count);
print_line(buffer);
if (error_count == 0) exit(0);
else exit(-SYNTAX_ERROR);
}
/*--------------------------------------------------------------*/
/* block Process a block, which consists of */
/* declarations followed by a compound */
/* statement. */
/*--------------------------------------------------------------*/
TOKEN_CODE follow_decls_list[] = {SEMICOLON, BEGIN, END_OF_FILE, 0};
block(rtn_idp)
SYMTAB_NODE_PTR rtn_idp; /* id of program or routine */
{
extern BOOLEAN block_flag;
declarations(rtn_idp);
/*
-- Error synchronization: Should be ;
*/
synchronize(follow_decls_list, NULL, NULL);
if (token != BEGIN) error(MISSING_BEGIN);
compound_statement();
}