[Feature] 写完了 打死不改了
This commit is contained in:
parent
3b735a43b8
commit
e92384f424
|
@ -233,6 +233,8 @@ namespace CompilerDesignIFlr1
|
|||
grammar.Add(subValue.Trim());
|
||||
else if (TokenUnit.ContainsKey(subValue.Trim()))
|
||||
grammar.Add(subValue.Trim());
|
||||
else if (subValue.StartsWith('(') && subValue.EndsWith(')'))
|
||||
grammar.Add(GrammarUnitCreate(subValue.Substring(1, subValue.Length - 2)));
|
||||
else
|
||||
{
|
||||
if (subValue.Trim().Length == 0)
|
||||
|
|
|
@ -49,9 +49,24 @@ namespace CompilerDesignIFlr1
|
|||
Grammar = grammar;
|
||||
}
|
||||
|
||||
public LR1Unit(int id, string type, string name, int quadrupleIndex, string value, HashSet<string> prospect, int pointPosition, List<string> grammar)
|
||||
{
|
||||
Id = id;
|
||||
Type = type;
|
||||
Name = name;
|
||||
QuadrupleIndex = quadrupleIndex;
|
||||
Value = value;
|
||||
Prospect = prospect;
|
||||
PointPosition = pointPosition;
|
||||
Grammar = grammar;
|
||||
}
|
||||
|
||||
internal LR1Unit Clone() =>
|
||||
new LR1Unit(Id, Type, Name, 0, Value,new HashSet<string>(Prospect), PointPosition, [.. Grammar]);
|
||||
internal int Id { get; set; } = -1;
|
||||
internal string Type { get; set; } = "";
|
||||
internal string Name { get; set; } = "";
|
||||
internal int QuadrupleIndex { get; set; } = 0;
|
||||
internal string Value { get; set; } = "";
|
||||
internal HashSet<string> Prospect { get; set; } = [];
|
||||
internal int PointPosition { get; set; } = 0;
|
||||
|
@ -93,9 +108,6 @@ namespace CompilerDesignIFlr1
|
|||
return sb.ToString();
|
||||
}
|
||||
|
||||
internal LR1Unit Clone() =>
|
||||
new LR1Unit(Id, Type, Name, new HashSet<string>(Prospect), PointPosition, [.. Grammar]);
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (obj is not LR1Unit other)
|
||||
|
|
|
@ -78,16 +78,16 @@ namespace CompilerDesignIflr1
|
|||
temp.Value = s;
|
||||
ans.Add(temp);
|
||||
}
|
||||
else if (Symbol.TryGetValue(s, out var symbol))
|
||||
else if(i + 1 < list.Count && Symbol.TryGetValue(s + list[i + 1], out var symbol))
|
||||
{
|
||||
var temp = symbol.Clone();
|
||||
if (i + 1 < list.Count && Symbol.TryGetValue(s + list[i + 1], out symbol))
|
||||
{
|
||||
temp = symbol.Clone();
|
||||
temp.Value = s + list[i + 1];
|
||||
i++;
|
||||
ans.Add(temp);
|
||||
}
|
||||
else
|
||||
else if (Symbol.TryGetValue(s, out symbol))
|
||||
{
|
||||
var temp = symbol.Clone();
|
||||
temp.Value = s;
|
||||
ans.Add(temp);
|
||||
}
|
||||
|
@ -96,7 +96,8 @@ namespace CompilerDesignIflr1
|
|||
bool noAnswer = true;
|
||||
foreach (var (pattern, ut) in Patterns)
|
||||
{
|
||||
if (Regex.IsMatch(s, pattern))
|
||||
|
||||
if (Regex.IsMatch(s, '^' + pattern + '$'))
|
||||
{
|
||||
var temp = ut.Clone();
|
||||
temp.Value = s;
|
||||
|
@ -118,7 +119,7 @@ namespace CompilerDesignIflr1
|
|||
{
|
||||
List<string> lt = new List<string>();
|
||||
int l = 0;
|
||||
int r = 1;
|
||||
int r = 0;
|
||||
|
||||
while (r < s.Length)
|
||||
{
|
||||
|
@ -130,8 +131,19 @@ namespace CompilerDesignIflr1
|
|||
lt.Add(k);
|
||||
}
|
||||
k = s[r].ToString().Trim();
|
||||
|
||||
if (k.Length != 0)
|
||||
{
|
||||
if(k is "\'" or "\"")
|
||||
{
|
||||
l = r;
|
||||
int temp = r + 1;
|
||||
while (temp < s.Length && s[temp].ToString().Trim() != k)
|
||||
temp++;
|
||||
lt.Add(s.Substring(l, temp - l + 1));
|
||||
r = temp;
|
||||
}
|
||||
else
|
||||
lt.Add(k);
|
||||
}
|
||||
l = r + 1;
|
||||
|
|
|
@ -7,4 +7,6 @@ var grammarReader = new GrammarReader("./files/if-grammar.grammar");
|
|||
var lr1Creator = new LR1Creator(grammarReader);
|
||||
var lr1Table = new LR1Table(lr1Creator);
|
||||
var lexicalAnalysis = new LexicalAnalysis(lr1Creator, "./files/code");
|
||||
var stateMachine = new StateMachine(lr1Table, lexicalAnalysis, lr1Creator);
|
||||
var semanticAnalysis = new SemanticAnalysis();
|
||||
var stateMachine = new StateMachine(lr1Table, lexicalAnalysis, lr1Creator, semanticAnalysis);
|
||||
semanticAnalysis.PrintQuadruples();
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -12,15 +12,17 @@ namespace CompilerDesignIFlr1
|
|||
internal LR1Table Table;
|
||||
internal LexicalAnalysis AnalysisResult;
|
||||
internal LR1Creator Creator;
|
||||
internal List<(LR1Unit, int)> stack = [(new LR1Unit("End","#"),0)];
|
||||
internal List<(LR1Unit, int)> _Stack = [(new LR1Unit("End","#"),0)];
|
||||
internal Stack<LR1Unit> Tokens;
|
||||
internal SemanticAnalysis SemanticAnalysis;
|
||||
|
||||
internal StateMachine(LR1Table table, LexicalAnalysis analysis, LR1Creator creator)
|
||||
internal StateMachine(LR1Table table, LexicalAnalysis analysis, LR1Creator creator, SemanticAnalysis semanticAnalysis)
|
||||
{
|
||||
Table = table;
|
||||
AnalysisResult = analysis;
|
||||
Creator = creator;
|
||||
Tokens = AnalysisResult.GetStack();
|
||||
SemanticAnalysis = semanticAnalysis;
|
||||
Compute();
|
||||
}
|
||||
|
||||
|
@ -35,23 +37,26 @@ namespace CompilerDesignIFlr1
|
|||
|
||||
internal bool ComputeOnce(LR1Unit unit)
|
||||
{
|
||||
var (action, destination) = Table.Rows[stack[^1].Item2].Next(unit.Name);
|
||||
var (action, destination) = Table.Rows[_Stack[^1].Item2].Next(unit.Name);
|
||||
switch (action)
|
||||
{
|
||||
case "GOTO":
|
||||
stack.Add((unit, destination));
|
||||
_Stack.Add((unit, destination));
|
||||
SemanticAnalysis.ShiftAnalysis(unit);
|
||||
break;
|
||||
case "Reduce":
|
||||
var reduceUnit = Creator.UnitIndex[destination];
|
||||
if (reduceUnit.CanReduce(stack.Select(x => x.Item1).ToList()))
|
||||
if (reduceUnit.CanReduce(_Stack.Select(x => x.Item1).ToList()))
|
||||
{
|
||||
stack.RemoveRange(stack.Count - reduceUnit.Grammar.Count, reduceUnit.Grammar.Count);
|
||||
var vals = _Stack.Skip(_Stack.Count - reduceUnit.Grammar.Count).Take(reduceUnit.Grammar.Count).Select(x=>x.Item1) .ToList();
|
||||
reduceUnit = SemanticAnalysis.Analysis(vals, reduceUnit.Clone());
|
||||
_Stack.RemoveRange(_Stack.Count - reduceUnit.Grammar.Count, reduceUnit.Grammar.Count);
|
||||
}
|
||||
else
|
||||
throw new Exception("Reduce not allow.");
|
||||
Console.WriteLine(reduceUnit);
|
||||
Tokens.Push(unit);
|
||||
Tokens.Push(reduceUnit.Clone());
|
||||
Tokens.Push(reduceUnit);
|
||||
break;
|
||||
case "ACC":
|
||||
Console.WriteLine(Creator.UnitIndex[destination]);
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
int i=0,t,b=15;
|
||||
if(i>=10)
|
||||
if(k==9)
|
||||
h = 6;
|
||||
else
|
||||
{
|
||||
c=10;
|
||||
if(k<=95 && )
|
||||
{
|
||||
int a =1*9+2;
|
||||
if(a>9){
|
||||
a=a+1;
|
||||
if(9>1){
|
||||
a=10;
|
||||
}
|
||||
a=2+2;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -3,17 +3,25 @@
|
|||
}
|
||||
|
||||
StatementList {
|
||||
Statement*
|
||||
LBrace Statement* RBrace
|
||||
}
|
||||
|
||||
@skip { Whitespace }
|
||||
|
||||
IfStatement {
|
||||
If LParen ConditionalExpression RParen PartIfStatement Else Statement | If LParen ConditionalExpression RParen Statement
|
||||
If ConditionPart PartIfStatement Else Statement | If ConditionPart Statement
|
||||
}
|
||||
|
||||
PartIfStatement {
|
||||
If LParen ConditionalExpression RParen PartIfStatement Else PartIfStatement | NoIfStatement
|
||||
If ConditionPart PartIfStatement Else PartIfStatement | NoIfStatement
|
||||
}
|
||||
|
||||
ConditionPart {
|
||||
LParen Condition RParen
|
||||
}
|
||||
|
||||
Condition {
|
||||
ConditionalExpression (LogicalOperator ConditionalExpression)*
|
||||
}
|
||||
|
||||
ConditionalExpression {
|
||||
|
@ -45,7 +53,7 @@ ConstantDefinition {
|
|||
}
|
||||
|
||||
VariableDefinition {
|
||||
Type (Identifier | AssignmentStatement)+ (Comma Identifier | Comma AssignmentStatement)*
|
||||
Type (Identifier | AssignmentStatement) (Comma Identifier | Comma AssignmentStatement)*
|
||||
}
|
||||
|
||||
Type {
|
||||
|
@ -69,7 +77,11 @@ Number {
|
|||
}
|
||||
|
||||
Operator {
|
||||
EuqalTo | NotEqualTo | LessThan | GreaterThan | LessThanOrEqual | GreaterThanOrEqual
|
||||
EqualTo | NotEqualTo | LessThan | GreaterThan | LessThanOrEqual | GreaterThanOrEqual
|
||||
}
|
||||
|
||||
LogicalOperator {
|
||||
And | Or
|
||||
}
|
||||
|
||||
@tokens {
|
||||
|
@ -93,7 +105,7 @@ Operator {
|
|||
UnsignedNumber { $[0-9]+ }
|
||||
String { "\"" $[\x00-\x7F]* "\"" }
|
||||
Character { "'" $[\x00-\x7F] "'" }
|
||||
EuqalTo { "==" }
|
||||
EqualTo { "==" }
|
||||
NotEqualTo { "!=" }
|
||||
LessThan { "<" }
|
||||
GreaterThan { ">" }
|
||||
|
@ -101,4 +113,6 @@ Operator {
|
|||
GreaterThanOrEqual { ">=" }
|
||||
Equal { "=" }
|
||||
Whitespace { $[\t\n\r]+ }
|
||||
And { "&&" }
|
||||
Or { "||" }
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
文法:
|
||||
key: Program
|
||||
0 Program ::= . StatementList
|
||||
key: StatementList
|
||||
1 StatementList ::= . LBrace Statement_0 RBrace
|
||||
key: IfStatement
|
||||
2 IfStatement ::= . If ConditionPart PartIfStatement Else Statement
|
||||
3 IfStatement ::= . If ConditionPart Statement
|
||||
key: PartIfStatement
|
||||
4 PartIfStatement ::= . If ConditionPart PartIfStatement Else PartIfStatement
|
||||
5 PartIfStatement ::= . NoIfStatement
|
||||
key: ConditionPart
|
||||
6 ConditionPart ::= . LParen Condition RParen
|
||||
key: Condition
|
||||
7 Condition ::= . ConditionalExpression LogicalOperator_ConditionalExpression_0
|
||||
key: ConditionalExpression
|
||||
8 ConditionalExpression ::= . Expression Operator Expression
|
||||
9 ConditionalExpression ::= . Expression
|
||||
key: Expression
|
||||
10 Expression ::= . Term AddLike_Term_0
|
||||
key: Statement
|
||||
11 Statement ::= . IfStatement
|
||||
12 Statement ::= . NoIfStatement
|
||||
key: NoIfStatement
|
||||
13 NoIfStatement ::= . AssignmentStatement Semicolon
|
||||
14 NoIfStatement ::= . VariableDefinition Semicolon
|
||||
15 NoIfStatement ::= . LBrace Statement_1 RBrace
|
||||
16 NoIfStatement ::= . ConstantDefinition Semicolon
|
||||
17 NoIfStatement ::= . Semicolon
|
||||
key: AssignmentStatement
|
||||
18 AssignmentStatement ::= . Identifier Equal Expression
|
||||
key: Term
|
||||
19 Term ::= . Factor MultiplyLike_Factor_0
|
||||
key: ConstantDefinition
|
||||
20 ConstantDefinition ::= . Const VariableDefinition
|
||||
key: VariableDefinition
|
||||
21 VariableDefinition ::= . Type Identifier__AssignmentStatement_0 Comma_Identifier__Comma_AssignmentStatement_0
|
||||
key: Type
|
||||
22 Type ::= . Int
|
||||
23 Type ::= . Char
|
||||
key: Factor
|
||||
24 Factor ::= . Identifier
|
||||
25 Factor ::= . Number
|
||||
26 Factor ::= . Character
|
||||
27 Factor ::= . LParen Expression RParen
|
||||
key: AddLike
|
||||
28 AddLike ::= . Plus
|
||||
29 AddLike ::= . Minus
|
||||
key: MultiplyLike
|
||||
30 MultiplyLike ::= . Multiply
|
||||
31 MultiplyLike ::= . Divide
|
||||
32 MultiplyLike ::= . Modulo
|
||||
key: Number
|
||||
33 Number ::= . UnsignedNumber
|
||||
34 Number ::= . Minus UnsignedNumber
|
||||
35 Number ::= . Plus UnsignedNumber
|
||||
key: Operator
|
||||
36 Operator ::= . EqualTo
|
||||
37 Operator ::= . NotEqualTo
|
||||
38 Operator ::= . LessThan
|
||||
39 Operator ::= . GreaterThan
|
||||
40 Operator ::= . LessThanOrEqual
|
||||
41 Operator ::= . GreaterThanOrEqual
|
||||
key: LogicalOperator
|
||||
42 LogicalOperator ::= . And
|
||||
43 LogicalOperator ::= . Or
|
||||
key: Statement_0
|
||||
44 Statement_0 ::= .
|
||||
45 Statement_0 ::= . Statement Statement_0
|
||||
key: LogicalOperator_ConditionalExpression_0
|
||||
46 LogicalOperator_ConditionalExpression_0 ::= .
|
||||
47 LogicalOperator_ConditionalExpression_0 ::= . LogicalOperator ConditionalExpression LogicalOperator_ConditionalExpression_0
|
||||
key: AddLike_Term_0
|
||||
48 AddLike_Term_0 ::= .
|
||||
49 AddLike_Term_0 ::= . AddLike Term AddLike_Term_0
|
||||
key: Statement_1
|
||||
50 Statement_1 ::= .
|
||||
51 Statement_1 ::= . Statement Statement_1
|
||||
key: MultiplyLike_Factor_0
|
||||
52 MultiplyLike_Factor_0 ::= .
|
||||
53 MultiplyLike_Factor_0 ::= . MultiplyLike Factor MultiplyLike_Factor_0
|
||||
key: Identifier__AssignmentStatement_0
|
||||
54 Identifier__AssignmentStatement_0 ::= . Identifier
|
||||
55 Identifier__AssignmentStatement_0 ::= . AssignmentStatement
|
||||
key: Comma_Identifier__Comma_AssignmentStatement_0
|
||||
56 Comma_Identifier__Comma_AssignmentStatement_0 ::= .
|
||||
57 Comma_Identifier__Comma_AssignmentStatement_0 ::= . Comma Identifier Comma_Identifier__Comma_AssignmentStatement_0
|
||||
58 Comma_Identifier__Comma_AssignmentStatement_0 ::= . Comma AssignmentStatement Comma_Identifier__Comma_AssignmentStatement_0
|
|
@ -0,0 +1,90 @@
|
|||
文法:
|
||||
key: Program
|
||||
0 Program ::= . StatementList
|
||||
key: StatementList
|
||||
1 StatementList ::= . LBrace Statement_0 RBrace
|
||||
key: IfStatement
|
||||
2 IfStatement ::= . If ConditionPart PartIfStatement Else Statement
|
||||
3 IfStatement ::= . If ConditionPart Statement
|
||||
key: PartIfStatement
|
||||
4 PartIfStatement ::= . If ConditionPart PartIfStatement Else PartIfStatement
|
||||
5 PartIfStatement ::= . NoIfStatement
|
||||
key: ConditionPart
|
||||
6 ConditionPart ::= . LParen Condition RParen
|
||||
key: Condition
|
||||
7 Condition ::= . ConditionalExpression LogicalOperator_ConditionalExpression_0
|
||||
key: ConditionalExpression
|
||||
8 ConditionalExpression ::= . Expression Operator Expression
|
||||
9 ConditionalExpression ::= . Expression
|
||||
key: Expression
|
||||
10 Expression ::= . Term AddLike_Term_0
|
||||
key: Statement
|
||||
11 Statement ::= . IfStatement
|
||||
12 Statement ::= . NoIfStatement
|
||||
key: NoIfStatement
|
||||
13 NoIfStatement ::= . AssignmentStatement Semicolon
|
||||
14 NoIfStatement ::= . VariableDefinition Semicolon
|
||||
15 NoIfStatement ::= . LBrace Statement_1 RBrace
|
||||
16 NoIfStatement ::= . ConstantDefinition Semicolon
|
||||
17 NoIfStatement ::= . Semicolon
|
||||
key: AssignmentStatement
|
||||
18 AssignmentStatement ::= . Identifier Equal Expression
|
||||
key: Term
|
||||
19 Term ::= . Factor MultiplyLike_Factor_0
|
||||
key: ConstantDefinition
|
||||
20 ConstantDefinition ::= . Const VariableDefinition
|
||||
key: VariableDefinition
|
||||
21 VariableDefinition ::= . Type Identifier__AssignmentStatement_0 Comma_Identifier__Comma_AssignmentStatement_0
|
||||
key: Type
|
||||
22 Type ::= . Int
|
||||
23 Type ::= . Char
|
||||
key: Factor
|
||||
24 Factor ::= . Identifier
|
||||
25 Factor ::= . Number
|
||||
26 Factor ::= . Character
|
||||
27 Factor ::= . LParen Expression RParen
|
||||
key: AddLike
|
||||
28 AddLike ::= . Plus
|
||||
29 AddLike ::= . Minus
|
||||
key: MultiplyLike
|
||||
30 MultiplyLike ::= . Multiply
|
||||
31 MultiplyLike ::= . Divide
|
||||
32 MultiplyLike ::= . Modulo
|
||||
key: Number
|
||||
33 Number ::= . UnsignedNumber
|
||||
34 Number ::= . Minus UnsignedNumber
|
||||
35 Number ::= . Plus UnsignedNumber
|
||||
key: Operator
|
||||
36 Operator ::= . EqualTo
|
||||
37 Operator ::= . NotEqualTo
|
||||
38 Operator ::= . LessThan
|
||||
39 Operator ::= . GreaterThan
|
||||
40 Operator ::= . LessThanOrEqual
|
||||
41 Operator ::= . GreaterThanOrEqual
|
||||
key: LogicalOperator
|
||||
42 LogicalOperator ::= . And
|
||||
43 LogicalOperator ::= . Or
|
||||
key: Statement_0
|
||||
44 Statement_0 ::= .
|
||||
45 Statement_0 ::= . Statement Statement_0
|
||||
key: LogicalOperator_ConditionalExpression_0
|
||||
46 LogicalOperator_ConditionalExpression_0 ::= .
|
||||
47 LogicalOperator_ConditionalExpression_0 ::= . LogicalOperator ConditionalExpression LogicalOperator_ConditionalExpression_0
|
||||
key: AddLike_Term_0
|
||||
48 AddLike_Term_0 ::= .
|
||||
49 AddLike_Term_0 ::= . AddLike Term AddLike_Term_0
|
||||
key: Statement_1
|
||||
50 Statement_1 ::= .
|
||||
51 Statement_1 ::= . Statement Statement_1
|
||||
key: MultiplyLike_Factor_0
|
||||
52 MultiplyLike_Factor_0 ::= .
|
||||
53 MultiplyLike_Factor_0 ::= . MultiplyLike Factor MultiplyLike_Factor_0
|
||||
key: Identifier__AssignmentStatement_0
|
||||
54 Identifier__AssignmentStatement_0 ::= . Identifier
|
||||
55 Identifier__AssignmentStatement_0 ::= . Identifier Identifier__AssignmentStatement_0
|
||||
56 Identifier__AssignmentStatement_0 ::= . AssignmentStatement
|
||||
57 Identifier__AssignmentStatement_0 ::= . AssignmentStatement Identifier__AssignmentStatement_0
|
||||
key: Comma_Identifier__Comma_AssignmentStatement_0
|
||||
58 Comma_Identifier__Comma_AssignmentStatement_0 ::= .
|
||||
59 Comma_Identifier__Comma_AssignmentStatement_0 ::= . Comma Identifier Comma_Identifier__Comma_AssignmentStatement_0
|
||||
60 Comma_Identifier__Comma_AssignmentStatement_0 ::= . Comma AssignmentStatement Comma_Identifier__Comma_AssignmentStatement_0
|
Loading…
Reference in New Issue