Skip to content

Commit

Permalink
PL0_Qt v1.0.0 高仿重制版
Browse files Browse the repository at this point in the history
  • Loading branch information
gdut-yy committed Jun 11, 2018
0 parents commit a1f7222
Show file tree
Hide file tree
Showing 19 changed files with 1,585 additions and 0 deletions.
8 changes: 8 additions & 0 deletions E01.PL0
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PROGRAM EX01;
VAR A,B,C;
BEGIN
A:=88;
B:=2;
C:=A+B*(3+B);
WRITE(C)
END.
33 changes: 33 additions & 0 deletions PL0_Qt.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#-------------------------------------------------
#
# Project created by QtCreator 2018-06-10T20:17:50
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = PL0_Qt
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
main.cpp \
mainwindow.cpp \
pl0.cpp

HEADERS += \
mainwindow.h

562 changes: 562 additions & 0 deletions PL0_Qt.pro.user

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# PL/0 编译器 高仿Qt重制版 v1.0.0

## 一、项目来源

本项目源于 本人大三下学期 编译原理课程 的课内实验和课程设计。课程用书是 清华大学出版社的《编译原理(第3版)》。

尽管当下已是8102年,微软刚刚完成对GitHub的收购,但这门课的 实验和课设 还依旧沿用着“祖传代码”,虽然比起教材后面的源码要好一点(真的只是一点,多了个GUI界面),但Borland C++Builder 6 这个编译器实在是太旧了,作为曾经 T1级别的编译器,在现在这个智能编译器满天飞的时代高下立见,附图为Borland C++Builder 6界面。在Windows 10 版本更新到 1803的今天,由于Borland C++Builder 6的兼容性问题,本人只能专门安装一个 Windows 7的虚拟机 去完成这个 实验和课设。

此刻本人已基本完成 实验和课设。由于在 Windows 7 的虚拟机的 Borland C++Builder 6 编译器上修改代码十分恶心!!!

所以特地花了一个下午用Qt写了这个 高仿重制版。除了重写了 GUI 和修复了几个小 BUG 以外,尽最大力度保持了与源码的一致。

![](demo.png)

![](qt00.png)

## 二、与 Borland C++Builder 6 对比(以 E01.PL0 为例)

### 1、E01.PL0 原文件(显示目标代码):

PROGRAM EX01;
VAR A,B,C;
BEGIN
A:=88;
READ(B);
C=A+B*(3+B);
WRITE(C)
END.

原版输出:

![](cbc01.png)

Qt版本输出:

![](qt01.png)

-----

### 2、E01.PL0 原文件(不显示目标代码):

原版输出:

![](cbc02.png)

Qt版本输出:

![](qt02.png)

-----

### 3、E01.PL0 原文件修正错误后:

PROGRAM EX01;
VAR A,B,C;
BEGIN
A:=88;
READ(B);
C:=A+B*(3+B);
WRITE(C)
END.

原版输出:

![](cbc03.png)

Qt版本输出:

![](qt03.png)

-----

### 4、输入数值2后,COD文件结果:

原版输出:

![](cbc04.png)

Qt版本输出:

![](qt04.png)
Binary file added cbc01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cbc02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cbc03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cbc04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.resize(1000,740);
w.show();

return a.exec();
}
111 changes: 111 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLayout>
#include <QBoxLayout>
#include <QGroupBox>
#include <QRadioButton>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QTextEdit>
#include <QGridLayout>
#include <QMessageBox>
#include <QFontDatabase>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent):QMainWindow(parent){
setWindowTitle(tr("(GDUT)PL0编译器 —— 高仿Qt重制版 v1.0.0 【GitHub 地址:https://github.com/gdut-yy/PL0_Qt 】")); //设置标题

createPL0Page();
setCentralWidget(pl0Page);
}

MainWindow::~MainWindow(){
}

void MainWindow::createPL0Page(){
pl0Page = new QWidget;

// 定义页面字体样式
QFont qfont;
qfont.setPointSize(30);
qfont.setFamily("黑体");
qfont.setBold(true);

QFont qfont1;
qfont1.setPointSize(30);
qfont1.setFamily("Courier New");
qfont1.setBold(true);

// QFontDatabase database;
// foreach (const QString &family, database.families())
// {
// qDebug()<<family;
// }

// 左部文本框
pl0TextEdit = new QTextEdit();
pl0TextEdit->setText(tr("***** PL/0 Compiler Demo *****"));
pl0TextEdit->setFont(qfont1);

// 右部
sourceLabel = new QLabel(tr("源程序名"));
sourceLabel->setFont(qfont);

sourceLineEdit = new QLineEdit(tr("E01"));
sourceLineEdit->setFont(qfont1);

isVisibleGroupBox = new QGroupBox(tr("目标代码"));
visibleRadioButton = new QRadioButton(tr("显示"));
invisibleRadioButton = new QRadioButton(tr("不显示"));
visibleRadioButton->setChecked(true);

QLayout *isVisibleLayout = new QVBoxLayout;
isVisibleLayout->addWidget(visibleRadioButton);
isVisibleLayout->addWidget(invisibleRadioButton);
isVisibleGroupBox->setLayout(isVisibleLayout);
isVisibleGroupBox->setFont(qfont);

runButton = new QPushButton(tr("RUN"));
connect(runButton, SIGNAL(clicked(bool)), this, SLOT(runClicked()));
runButton->setFont(qfont);

QGridLayout *pageLayout = new QGridLayout;
pageLayout->setColumnStretch(0,5);
pageLayout->setColumnStretch(1,1);
pageLayout->setRowStretch(0,1); // label
pageLayout->setRowStretch(1,1); // lineedit
pageLayout->setRowStretch(2,1);
pageLayout->setRowStretch(3,5); // groupbox
pageLayout->setRowStretch(4,1);
pageLayout->setRowStretch(5,2); // button
pageLayout->setRowStretch(6,10);
pageLayout->addWidget(pl0TextEdit,0,0,7,1);
pageLayout->addWidget(sourceLabel,0,1,1,1);
pageLayout->addWidget(sourceLineEdit,1,1,1,1);
pageLayout->addWidget(isVisibleGroupBox,3,1,1,1);
pageLayout->addWidget(runButton,5,1,1,1);
pl0Page->setLayout(pageLayout);
}

void MainWindow::pl0printf(QString s){
pl0TextEdit->moveCursor(QTextCursor::End);
pl0TextEdit->insertPlainText("\n"+s);
}

void MainWindow::printfs(const char* s){
QString qs=QString(s);
pl0printf(qs);
}
void MainWindow::printcs(const char c){
QString qs=QString(c);
pl0printf(qs);
}
void MainWindow::printls(const char *s0, long v){
QString qs=QString(s0)+QString::number(v);
pl0printf(qs);
}
void MainWindow::printrs(const char *s0, float v){
QString qs=QString(s0)+QString("%1").arg(0);
pl0printf(qs);
}
71 changes: 71 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <cstring>
class QTextEdit;
class QGroupBox;
class QRadioButton;
class QLabel;
class QLineEdit;
class QPushButton;
class QLayout;

extern char *SYMOUT[];
typedef int *SYMSET; // SET OF SYMBOL;
typedef char ALFA[11];
typedef enum { CONSTANT, VARIABLE, PROCEDUR } OBJECTS ;
typedef enum { LIT, OPR, LOD, STO, CAL, INI, JMP, JPC } FCT;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
MainWindow *Form1;

void printfs(const char* s);
void printcs(const char c);
void printls(const char *s0, long v);
void printrs(const char *s0, float v);
void pl0printf(QString s);

void Error(int n);
void GetCh();
void GetSym();
void GEN(FCT X, int Y, int Z);
void TEST(SYMSET S1, SYMSET S2, int N);
void ENTER(OBJECTS K, int LEV, int &TX, int &DX);
int POSITION(ALFA ID, int TX);
void ConstDeclaration(int LEV,int &TX,int &DX);
void VarDeclaration(int LEV,int &TX,int &DX);
void ListCode(int CX0);
void FACTOR(SYMSET FSYS, int LEV, int &TX);
void TERM(SYMSET FSYS, int LEV, int &TX);
void EXPRESSION(SYMSET FSYS, int LEV, int &TX);
void CONDITION(SYMSET FSYS,int LEV,int &TX);
void STATEMENT(SYMSET FSYS,int LEV,int &TX);
void Block(int LEV, int TX, SYMSET FSYS);
int BASE(int L,int B,int S[]);
void Interpret();

private:
void createPL0Page(); // 定义创建 PL0编译器 页面函数

QWidget *pl0Page;
QTextEdit *pl0TextEdit; // 左文本框
QLabel *sourceLabel;
QLayout *rightLayout;
QLineEdit *sourceLineEdit;
QGroupBox *isVisibleGroupBox;
QRadioButton *visibleRadioButton;
QRadioButton *invisibleRadioButton;
QPushButton *runButton;

private slots:
void runClicked();
};

#endif // MAINWINDOW_H
24 changes: 24 additions & 0 deletions mainwindow.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>MainWindow</string>
</property>
<widget class="QMenuBar" name="menuBar" />
<widget class="QToolBar" name="mainToolBar" />
<widget class="QWidget" name="centralWidget" />
<widget class="QStatusBar" name="statusBar" />
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</ui>
Loading

0 comments on commit a1f7222

Please sign in to comment.