Skip to content

Commit 09d75ae

Browse files
committed
1.修复错误高亮中出现高亮多余字符的问题
1 parent 10bfa1e commit 09d75ae

File tree

10 files changed

+86
-39
lines changed

10 files changed

+86
-39
lines changed

SyntacticAnalyzer/SyntacticAnalyzer/AnalysisStep.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ void AnalysisStep::add_step(map<int, vector<string>> setp) {
1818
int col = 0;
1919
for (it; it != end; it++) {
2020
int size = it->second.size();
21-
//qDebug() << "size=" << QString("%1").arg(size);
22-
//setp_model->setHeaderData(row, Qt::Vertical, QString("%1").arg(it->first));
2321
col = 0;
2422
setp_model->setItem(row, col, new QStandardItem(QString("%1").arg(it->first)));
2523
str_it = it->second.begin();
@@ -42,12 +40,11 @@ AnalysisStep::~AnalysisStep() {
4240
}
4341

4442
void AnalysisStep::init_table() {
45-
setp_model->setColumnCount(5);
46-
//setp_model->setHeaderData(0, Qt::Horizontal, QString::fromLocal8Bit("步骤"));
43+
setp_model->setColumnCount(5);;
4744
setp_model->setHeaderData(0, Qt::Horizontal, QString::fromLocal8Bit("符号栈状态"));
4845
setp_model->setHeaderData(1, Qt::Horizontal, QString::fromLocal8Bit("关系"));
4946
setp_model->setHeaderData(2, Qt::Horizontal, QString::fromLocal8Bit("输入串状态"));
50-
setp_model->setHeaderData(3, Qt::Horizontal, QString::fromLocal8Bit("最短素短语"));
47+
setp_model->setHeaderData(3, Qt::Horizontal, QString::fromLocal8Bit("最左素短语"));
5148
setp_model->setHeaderData(4, Qt::Horizontal, QString::fromLocal8Bit("动作"));
5249
ui.setp_table->setModel(setp_model);
5350
ui.setp_table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);

SyntacticAnalyzer/SyntacticAnalyzer/SyntacticAnalyzer.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ SyntacticAnalyzer::SyntacticAnalyzer(QWidget *parent)
1919

2020
void SyntacticAnalyzer::init() {
2121
settings = new QSettings("config.ini", QSettings::IniFormat);
22-
get_settings(); //读取配置信息
22+
get_settings();
2323
cfg = new Config();
2424
input = new InputInfo();
2525
last_first_model = new TableModel;
@@ -28,11 +28,11 @@ void SyntacticAnalyzer::init() {
2828
info_type = -1;
2929
ui.lastView->setEditTriggers(QAbstractItemView::NoEditTriggers);
3030
ui.tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
31-
update_input(); //更新信息
32-
exp_check.get_grammar(grammars); //获取文法
33-
exp_check.get_terminal_symbol(terminal_symbols); //获取终结符集合
34-
exp_check.make_priority_table(); //生成优先关系表
35-
connect_signal_slot(); //连接信号与槽
31+
update_input();
32+
exp_check.get_grammar(grammars);
33+
exp_check.get_terminal_symbol(terminal_symbols);
34+
exp_check.make_priority_table();
35+
connect_signal_slot();
3636
set_first_last();
3737
set_prority_table();
3838
show_last_first();
@@ -94,7 +94,6 @@ void SyntacticAnalyzer::connect_signal_slot() {
9494
connect(input, SIGNAL(finish()), this, SLOT(input_finish()));
9595
connect(ui.output_text, SIGNAL(anchorClicked(const QUrl&)), this, SLOT(show_step(const QUrl&)));
9696
}
97-
9897
SyntacticAnalyzer::~SyntacticAnalyzer() {
9998
delete settings;
10099
delete cfg;
@@ -554,27 +553,39 @@ void SyntacticAnalyzer::check_expression(string exp, int record_index) {
554553
map<int, string> error = exp_check.get_error();
555554
map<int, string>::iterator it = error.begin();
556555
map<int, string>::iterator end = error.end();
557-
bool flag = true;
558556
QString error_info = "";
559557
if (error.empty()) //表达式生成超链接
560558
ui.output_text->append(QString("<html><p><a href=\"goto://setp?record_index=%1\">%2</a></p></html>").arg(record_index).arg(QString::fromStdString(exp)));
561559
else
562560
ui.output_text->append(QString::fromStdString(exp));
561+
qDebug() << QString::fromStdString(exp);
563562
for (it; it != end; it++) { //高亮错误
564-
flag = false;
565563

566564
QTextCursor cursor(ui.output_text->document());
567565
QTextCursor cursor_postion(ui.output_text->textCursor());
568566
QTextCharFormat color_format;
569567
color_format.setForeground(Qt::red);
570568
int postion = cursor_postion.position() - (exp.length() - it->first);
571-
cursor.setPosition(postion, QTextCursor::KeepAnchor);
572-
cursor.movePosition(QTextCursor::NoMove, QTextCursor::KeepAnchor, 1);
569+
cursor.setPosition(postion - 1, QTextCursor::MoveAnchor);
570+
cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor, 0);
571+
cursor.insertText(" ");
572+
cursor.setPosition(postion + 1, QTextCursor::MoveAnchor);
573+
cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor, 0);
574+
cursor.insertText(" ");
575+
postion = cursor_postion.position() - (exp.length() + 2 - it->first);
576+
cursor.setPosition(postion, QTextCursor::MoveAnchor);
577+
cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor, 1);
573578
cursor.select(QTextCursor::WordUnderCursor);
574-
cursor.setCharFormat(color_format);
579+
cursor.mergeCharFormat(color_format);
580+
//去掉空格
581+
cursor.setPosition(postion - 1, QTextCursor::MoveAnchor);
582+
cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor, 0);
583+
cursor.deleteChar();
584+
cursor.setPosition(postion, QTextCursor::MoveAnchor);
585+
cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor, 0);
586+
cursor.deleteChar();
575587
}
576588
for (it = error.begin(); it != end; it++) { //错误记录生成超链接
577-
flag = false;
578589

579590
ui.output_text->append(QString("<html><p><a href=\"goto://setp?record_index=%1\">%2:%3</a></p></html>").arg(record_index).arg(it->first).arg(QString::fromLocal8Bit(it->second.data())));
580591
}

SyntacticAnalyzer/SyntacticAnalyzer/SyntacticAnalyzer.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@
3232
#include <QMessageBox>
3333
using namespace std;
3434

35-
///=================================================================================================
36-
/// \class SyntacticAnalyzer
37-
///
38-
/// \brief A syntactic analyzer.
39-
///
40-
/// \date 2018/6/7
41-
///=================================================================================================
4235

4336
class SyntacticAnalyzer : public QMainWindow {
4437
Q_OBJECT
@@ -129,6 +122,7 @@ class SyntacticAnalyzer : public QMainWindow {
129122
/// \brief 分析过程
130123
AnalysisStep *analysis_setp;
131124

125+
132126
/// \brief 输入的信息类型,0为文法,1为表达式,2为终结符
133127
int info_type;
134128

SyntacticAnalyzer/SyntacticAnalyzer/SyntacticAnalyzer.vcxproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
3333
<LibraryPath>$(ProjectDir)\lib;D:\QT\QT\5.9\MSVC2017_64\lib;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64</LibraryPath>
3434
<IncludePath>$(ProjectDir)\include;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
35+
<ExecutablePath>$(ProjectDir)\lib;$(VC_ExecutablePath_x64);$(WindowsSDK_ExecutablePath);$(VS_ExecutablePath);$(MSBuild_ExecutablePath);$(FxCopDir);$(PATH);</ExecutablePath>
3536
</PropertyGroup>
3637
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
3738
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
3839
<LibraryPath>$(ProjectDir)\lib;D:\QT\QT\5.9\MSVC2017_64\lib;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(NETFXKitsDir)Lib\um\x64</LibraryPath>
3940
<IncludePath>D:\QT\QT\5.9\MSVC2017_64;$(ProjectDir)\include;$(VC_IncludePath);$(WindowsSDK_IncludePath);</IncludePath>
4041
<ReferencePath>$(VC_ReferencesPath_x64);D:\QT\QT\5.9\MSVC2017_64</ReferencePath>
42+
<ExecutablePath>$(ProjectDir)\lib;$(VC_ExecutablePath_x64);$(WindowsSDK_ExecutablePath);$(VS_ExecutablePath);$(MSBuild_ExecutablePath);$(FxCopDir);$(PATH);</ExecutablePath>
4143
</PropertyGroup>
4244
<Target Name="QtMsBuildNotFound" BeforeTargets="CustomBuild;ClCompile" Condition="!Exists('$(QtMsBuild)\qt.targets') or !Exists('$(QtMsBuild)\qt.props')">
4345
<Message Importance="High" Text="QtMsBuild: could not locate qt.targets, qt.props; project may not build correctly." />
@@ -70,6 +72,7 @@
7072
<AdditionalLibraryDirectories>$(ProjectDir)\lib;$(QTDIR)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
7173
<GenerateDebugInformation>true</GenerateDebugInformation>
7274
<AdditionalDependencies>operator_precedence_analysisd.lib;libboost_regex-vc141-mt-gd-x64-1_67.lib;D:\QT\QT\5.9\MSVC2017_64\lib\qtmaind.lib;shell32.lib;E:\LIB\boost_1_67_0\lib64-msvc-14.1\libboost_regex-vc141-mt-gd-x64-1_67.lib;D:\QT\QT\5.9\MSVC2017_64\lib\Qt5Widgetsd.lib;D:\QT\QT\5.9\MSVC2017_64\lib\Qt5Guid.lib;D:\QT\QT\5.9\MSVC2017_64\lib\Qt5Cored.lib</AdditionalDependencies>
75+
<AdditionalOptions>/ENTRY:"mainCRTStartup" %(AdditionalOptions)</AdditionalOptions>
7376
</Link>
7477
<QtMoc>
7578
<OutputFile>.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp</OutputFile>
@@ -103,7 +106,7 @@
103106
<AdditionalLibraryDirectories>$(ProjectDir)\lib;$(QTDIR)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
104107
<GenerateDebugInformation>false</GenerateDebugInformation>
105108
<AdditionalDependencies>libboost_regex-vc141-mt-x64-1_67.lib;operator_precedence_analysis.lib;Qt5Core.lib;Qt5Gui.lib;Qt5Widgets.lib</AdditionalDependencies>
106-
<AdditionalOptions>/ENTRY:"mainCRTStartup" /LTCG %(AdditionalOptions)</AdditionalOptions>
109+
<AdditionalOptions>/ENTRY:"mainCRTStartup" %(AdditionalOptions)</AdditionalOptions>
107110
</Link>
108111
<QtMoc>
109112
<OutputFile>.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp</OutputFile>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
+
2+
-
3+
*
4+
/
5+
(
6+
)
7+
i
8+
9+
E->E+T|E-T|T
10+
T->T*F|T/F|F
11+
F->(E)|i
12+
13+
(i+i*i)*i-i
14+
(i+i*i*i-i
15+
(i+(i*i))/(i-i)
16+
(i+i-i))*i-i
17+
(i+(i*i))i
18+
(i*i)*i
19+
(i*i)(i)+i/ii
20+
21+
22+
+
23+
*
24+
(
25+
)
26+
i
27+
28+
E->E+T|T
29+
T->T*F|F
30+
F->(E)|i
31+
32+
(i+i)*i
33+
(i+i*i
34+
i+i)*i
35+
((i+i)*(i+i))*i
36+
(i+i)i
37+
(i*i)+ii
38+

SyntacticAnalyzer/SyntacticAnalyzer/include/ExpressionCheck.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "stdafx.h"
12
#include "ExpressionCheck.h"
23
#include <regex>
34
#include <iterator>
@@ -9,10 +10,10 @@ ExpressionCheck::ExpressionCheck() {
910
error_info.push_back("缺少表达式"); //eg.
1011
error_info.push_back("表达式间缺少运算符"); //eg .ii
1112
error_info.push_back("括号间无表达式"); //eg.()
12-
error_info.push_back("非法左括号");
13+
error_info.push_back("缺少右括号");
1314
error_info.push_back("表达式间缺少运算符"); //eg. )(
1415
error_info.push_back("正确");
15-
error_info.push_back("非法右括号");
16+
error_info.push_back("缺少左括号");
1617
expression_index = 0;
1718
setp = 1;
1819
}
@@ -259,10 +260,6 @@ bool ExpressionCheck::match_production_left(string right_str) {
259260

260261
void ExpressionCheck::save_analysis_step(int index, char prioity, string left, string act) {
261262
string stack_status = get_stack_status();
262-
if (prioity == 0)
263-
prioity = '<';
264-
boost::format fmt("%-20s|%-20s|%-20s|%-20s|%-20s|%-20s");
265-
fmt % setp % stack_status % prioity % expression_str.substr(index) % left % act;
266263
analysis_info[setp].push_back(stack_status);
267264
analysis_info[setp].push_back(string("").append(1, prioity));
268265
analysis_info[setp].push_back(expression_str.substr(index));
@@ -271,6 +268,7 @@ void ExpressionCheck::save_analysis_step(int index, char prioity, string left, s
271268
setp++;
272269
}
273270

271+
274272
///=================================================================================================
275273
/// \fn int ExpressionCheck::check_expression(string expression)
276274
///

SyntacticAnalyzer/SyntacticAnalyzer/include/ExpressionCheck.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include<set>
1212
#include <map>
1313
#include <stack>
14+
#include <cctype>
15+
#include <boost/algorithm/string/replace.hpp>
1416
using namespace std;
1517

1618
///=================================================================================================
@@ -22,25 +24,25 @@ using namespace std;
2224

2325
enum ERROR_CODE {
2426

25-
///< 运算符间缺少表达式
27+
/// \brief 运算符间缺少表达式
2628
EXPRESSION_ERROR,
2729

28-
///< 表达式间缺少运算符
30+
/// \brief 表达式间缺少运算符
2931
EXP_NO_OPERATOR,
3032

31-
///< 括号间无表达式
33+
/// \brief 括号间无表达式
3234
BRACKETS_NO_EXP,
3335

34-
///< 非法左括号
36+
/// \brief 非法左括号
3537
INVALID_LEFT_BRACKET,
3638

37-
///< 表达式间缺少运算符
39+
/// \brief 表达式间缺少运算符
3840
DEFECT_OPERATOR,
3941

40-
///< 正确
42+
/// \brief 正确
4143
OK,
4244

43-
///< 非法右括号
45+
/// \brief 非法右括号
4446
INVALID_RIGHT_BRACKET
4547
};
4648

@@ -100,6 +102,7 @@ class ExpressionCheck {
100102

101103
//获取符号栈状态
102104
string get_stack_status();
105+
103106
~ExpressionCheck();
104107
private:
105108

@@ -169,6 +172,9 @@ class ExpressionCheck {
169172
//存储分析步骤
170173
void save_analysis_step(int index, char prioity, string left, string act);
171174

175+
176+
177+
172178
};
173179
#endif
174180

Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)