1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using EvalFinale . Grammar ;
5+
6+ using static EvalFinale . Traductor ;
7+
8+ namespace EvalFinale . Analyzer
9+ {
10+ public static class Analyze
11+ {
12+ public static char [ ] SEPARATORS = { ' ' , '\n ' , '\t ' } ;
13+
14+ public static ( List < string > , List < string > ) LexicalAnalysis ( string code , Language language )
15+ {
16+ ILanguage l = language switch
17+ {
18+ Language . Java => new Java ( ) ,
19+ Language . CS => new CS ( ) ,
20+ Language . PHP => new PHP ( ) ,
21+ _ => throw new ArgumentOutOfRangeException ( nameof ( language ) , language , null )
22+ } ;
23+
24+ return l . LexicalAnalysis ( code ) ;
25+ }
26+
27+ public static Node SyntaxicAnalysis ( List < string > lexems , List < Rule > rules , Dictionary < Symbol , Dictionary < Symbol , Rule > > table )
28+ {
29+ Symbol actual = rules [ 0 ] . Left ;
30+
31+ Node tree = new Node ( actual ) ;
32+ Node node = tree . Copy ( ) ;
33+ int i = 0 ;
34+ int cut = 0 ;
35+
36+ while ( i < lexems . Count && cut < 1000 )
37+ {
38+ string lexem = lexems [ i ] ;
39+ Symbol lexemT = new Symbol ( lexem ) ;
40+ if ( actual . IsTerminal )
41+ {
42+ if ( actual != new Symbol ( "ε" ) && actual != lexemT )
43+ throw new Exception ( "My Syntax Error: " + lexemT + " instead of " + actual ) ;
44+
45+ if ( actual == lexemT )
46+ i ++ ;
47+
48+ node = tree . Next ( node ) ;
49+ actual = node . Value ;
50+ }
51+ else
52+ {
53+ if ( ! table [ actual ] . ContainsKey ( lexemT ) )
54+ throw new Exception ( "My Syntax Error: no rule from " + actual ) ;
55+
56+
57+ Rule rule = table [ actual ] [ lexemT ] ;
58+
59+ foreach ( Symbol symbol in rule . Expression )
60+ node . AddChildren ( new Node ( symbol ) ) ;
61+
62+ node = node . Children [ 0 ] ;
63+ actual = node . Value ;
64+ }
65+
66+ cut ++ ;
67+ }
68+
69+ return tree ;
70+ }
71+
72+ private class Java : ILanguage
73+ {
74+ private readonly Dictionary < string , string > _reserved = new Dictionary < string , string >
75+ {
76+ { "int" , "type" } ,
77+ { "bool" , "type" } ,
78+ { "true" , "val" } ,
79+ { "=" , "=" } ,
80+ { ";" , ";" } ,
81+ { "(" , "(" } ,
82+ { ")" , ")" } ,
83+ { "{" , "{" } ,
84+ { "}" , "}" } ,
85+ { "while" , "while" } ,
86+ { "if" , "if" } ,
87+ { "else" , "else" } ,
88+ { "<" , "<" } ,
89+ { ">" , ">" } ,
90+ { "+" , "+" } ,
91+ { "-" , "-" } ,
92+ { "*" , "*" } ,
93+ { "/" , "/" } ,
94+ { "%" , "%" } ,
95+ { "System.out.println" , "id" }
96+ } ;
97+
98+ private List < string > _words ;
99+ private List < string > _lexems ;
100+
101+ private int _i ;
102+ private string _builtWord ;
103+
104+ public ( List < string > , List < string > ) LexicalAnalysis ( string code )
105+ {
106+ _words = new List < string > ( ) ;
107+ _lexems = new List < string > ( ) ;
108+
109+ _i = 0 ;
110+ _builtWord = "" ;
111+
112+ while ( _i < code . Length )
113+ {
114+ if ( SEPARATORS . Contains ( code [ _i ] ) )
115+ {
116+ if ( _builtWord . Length != 0 )
117+ {
118+ _words . Add ( _builtWord ) ;
119+ _lexems . Add ( decimal . TryParse ( _builtWord , out _ ) || bool . TryParse ( _builtWord , out _ ) ? "val" : "id" ) ;
120+ }
121+
122+ _builtWord = "" ;
123+ _i ++ ;
124+ }
125+ else
126+ {
127+ string foundWord = null ;
128+ string foundLexem = null ;
129+
130+ foreach ( string key in _reserved . Keys )
131+ {
132+ if ( _i + key . Length > code . Length )
133+ continue ;
134+
135+ if ( key == code . Substring ( _i , key . Length ) )
136+ {
137+ foundWord = key ;
138+ foundLexem = _reserved [ key ] ;
139+ break ;
140+ }
141+ }
142+
143+ if ( foundWord is null )
144+ {
145+ _builtWord += code [ _i ] ;
146+ _i ++ ;
147+ }
148+ else
149+ {
150+ if ( _builtWord . Length != 0 )
151+ {
152+ _words . Add ( _builtWord ) ;
153+ _lexems . Add ( decimal . TryParse ( _builtWord , out _ ) || bool . TryParse ( _builtWord , out _ ) ? "val" : "id" ) ;
154+ _builtWord = "" ;
155+ }
156+
157+ _words . Add ( foundWord ) ;
158+ _lexems . Add ( foundLexem ) ;
159+
160+ _i += foundWord . Length ;
161+ }
162+ }
163+ }
164+
165+ return ( _words , _lexems ) ;
166+ }
167+ }
168+
169+ private class CS : ILanguage
170+ {
171+ public ( List < string > , List < string > ) LexicalAnalysis ( string code )
172+ {
173+ throw new NotImplementedException ( ) ;
174+ }
175+ }
176+
177+ private class PHP : ILanguage
178+ {
179+ public ( List < string > , List < string > ) LexicalAnalysis ( string code )
180+ {
181+ throw new NotImplementedException ( ) ;
182+ }
183+ }
184+ }
185+ }
0 commit comments