Presentation is loading. Please wait.

Presentation is loading. Please wait.

nested-name-specifier

Similar presentations


Presentation on theme: "nested-name-specifier"— Presentation transcript:

1 nested-name-specifier
Kei Hasegawa

2 Look up symbol table while syntax analysis at C language
For regular expression [_a-zA-Z][_a-zA-Z0-9]* (i.e. name) string Should look up symbol table or not? Roughly say, if the type is required at the point, it’s necessary to look up symbol table for searching `typedef-name’ C Compiler (PDF Document) 2.2.1 Lexical analyzer and symbol table

3 And more `::’ at C++ namespace N { } N::outer::inner noi;
struct outer { // ... struct inner { static int si; }; struct S { /* ... */ }; extern double S; // ok : Variable name space separates from tag name space } N::outer::inner noi; int N::outer::inner::si; int N; // error: multiple definition of `N’ double N::S; struct N::S ns;

4 Part of C++ grammar nested-name-specifier: class-or-namespace-name:
class-or-namespace-name :: nested-name-specifier class-or-namespace-name :: ... class-or-namespace-name: class-name namespace-name type-name:

5 Name -> Token For name X , judge previous token is `::’ and next token is `::’ If previous token is `::’, look up symbol table for X If not exists, it’s immediately error. If next token is `::’, look up symbol table for X If next token is again name (call Y ) , Not looking up symbol table for Y , and determine the token of Y later. Apply other “Name -> Token rule” for X Suitably look up symbol table.

6 N::outer::inner noi; Next token of `N’ is `::’, so look up symbol table for `N’ `N’ is recognized as namespace-name Previous token of `outer’ is `::’, so look up symbol table for `outer’ `outer’ is recognized as class-name `outer ::’ are reduced as nested-name-specifier Previous token of `inner’ is `::’, so look up symbol table for `inner’ `inner’ is recognized as class-name `inner’ is reduced as type-name unlike `outer’ Previous token of `noi’ is not `::’, and next token of `noi’ is not `::’ either. So the other rule is applied: In this case, there already exists type at stack of decl_spec, so not look up symbol table

7 Move to scope When class-or-namespace-name is reduced, move to scope:
/* yacc/bison */ class_or_namespace_name : class_name { scope::current = $1; } | namespace_name { scope::current = $1; }

8 Move to scope (Continue)
Bellow code doesn’t work: nested-name-specifier: class-or-namespace-name :: { scope::current = ...; } class-or-namespace-name :: { scope::current = ...; } nested-name-specifier ... Later than previous sheet: cannot look up `outer’ nor `inner’ Moving to scope before `::’ is correct: i.e. same as previous sheet.

9 shift/reduce conflict handling
Report file of yacc/bison State X1 R11 nested_name_specifier: class_or_namespace_name COLONCOLON . nested_name_specifier R | class_or_namespace_name COLONCOLON . ... It’s not correct that parser always shifts every class-name token at State X1 Insert bellow code into suitable position of `yyparse’ : if (yystate == X1 && peek() != COLONCOLON) { yyn = R12 + 1; goto yyreduce; }

10 reduce/reduce conflict handling
Report file of yacc/bison State X2 R21 type_name: class_name . R22 class_or_namespace_name: class_name . The rule R22 is not used at this rate. Insert bellow code into suitable position of `yyparse’ : if (yystate == X2 && peek() == COLONCOLON) { yyn = R22 + 1; goto yyreduce; }

11 Postprocessing for `::’ moving scope
Required restoring the original scope N::outer::inner noi; At this point, scope is root. At this point, move to root from `N::outer’

12 At this point, scope is parameter scope of N::T::f
At this point, move to parameter scope of N::T::f At this point, scope is root void N::T::f ( N2::T2* p) { N3::T3::x を参照; At this point, move to body of N::T::f At this point, scope is body of N::T::f } At this point, move to root


Download ppt "nested-name-specifier"

Similar presentations


Ads by Google